Number generator problem...

J

jerryau

Hi,

I'm creating a number generator program, that is supposed to generate
6 unique random numbers for each game. I want to generate this for 6
games. The problem is, that it works for the first game, but then
all the other games have exactly the same numbers (e.g. Game 1:
1,2,3,4,5,6.. Game 2: 1,2.3,4,5,6). I don't know how to get delete
the previous array with those numbers, and then generate a new one.
Here is my code:

NumGenerator.cpp (Main class):
// This is the main project file for VC++
application project
// generated using an Application Wizard.

#include "stdafx.h"
#include "PowerGen.h"

#using <mscorlib.dll>

using namespace System;

int _tmain()
{
for (int i = 1; i <= 6; i++) {
Console::Write(S"Game ");
Console::Write(i);
Console::WriteLine(S":");
Console::WriteLine(S"-------");

int * pwNum = new int[];
PowerGen * pw = new PowerGen();
pwNum = pw->getNum();

for (int x = 0; x < 6; x++) {
Console::Write(pwNum[x]);
Console::Write(S", ");
}

Console::WriteLine();
Console::WriteLine();

delete pwNum;
delete pw;
}

return 0;
}

PowerGen.h:
[code:1:bb235d45c8]#using <mscorlib.dll>

__gc class PowerGen {
public:
PowerGen();
~PowerGen();
int* getNum();

private:
bool contains(int* n, int y, int size);
int* allNum;
};[/code:1:bb235d45c8]

PowerGen.cpp (This is where the random generating happens):
[code:1:bb235d45c8]#include "stdafx.h"
#include "PowerGen.h"

using namespace System;

PowerGen::powerGen() {
allNum = new int[6];

}

int* PowerGen::getNum() {

Random* r = new Random();
int i = 0;
int temp = 0;
while (i < 6) {
temp = r -> Next(1,45);

Console::Write(S"Temp");
Console::WriteLine(temp);

if (contains(allNum, temp, i)) {
temp = r -> Next(1,45);
} else {
Console::Write(S"Old: ");
Console::WriteLine(allNum);
Console::WriteLine(S"Writing new...");
allNum = temp;
i++;
}
}

return allNum;
}

bool PowerGen::contains(int* n, int y, int size)
{

for (int i = 0; i < size; i++) {
if (n == y) {
return true;
}
}
return false;
}

PowerGen::~PowerGen() {
}[/code:1:bb235d45c8]

Thanks,
Jerry
 
T

TT \(Tom Tempelaere\)

jerryau said:
Hi,

I'm creating a number generator program, that is supposed to generate
6 unique random numbers for each game. I want to generate this for 6
games. The problem is, that it works for the first game, but then
all the other games have exactly the same numbers (e.g. Game 1:
1,2,3,4,5,6.. Game 2: 1,2.3,4,5,6). I don't know how to get delete
the previous array with those numbers, and then generate a new one.
Here is my code: [...]
Thanks,
Jerry

You should not forget to seed the random number generator (srand), before
generating any random number using rand().

srand( (unsigned)time( NULL ) );

Don't forget that you must be careful that each number has an equal chance
of appearing. The following generates a random integer between 0 and n-1,
with the chance of each number appearing ~ (1 / n).

int rand_n(int n)
{
if(n<=0 || n>RAND_MAX)
return -1;
int bucket_size = RAND_MAX / n;
int r;
do
r = rand() / bucket_size;
while( r >= n );
return r;
}

You can then perhaps make a random array class.

// random numbers from 1..rand_max
class RandArray
{
int rand_max;
std::vector<int> random_numbers;
public:
RandArray(int size, int rand_max_)
: random_numbers(size), rand_max(0)
{ Regenerate(rand_max_); }

int GetSize() const { return random_numbers.size(); }
int GetRandMax() const { return rand_max; }
int operator[](int i) const { return random_numbers; }

void Regenerate(int rand_max_=-1)
{
if(rand_max_>= 0)
rand_max = rand_max_;
for(int i=0; i!=size; ++i)
random_numbers = 1 + rand_n(rand_max);
}
};

If you need to have new random numbers for a new game, call Regenerate on
the RandomArray object.

Tom.
 
T

TT \(Tom Tempelaere\)

Sorry, I missed the word unique. The implementation of RandArray::Regenerate
would be totally different. I would suggest using random_shuffle from
<algorithm>.

class RandArray
{
std::vector<int> random_numbers;
public:
RandArray(int size)
{
random_numbers.reserve(size);
for(int i=0; i!=size; ++i)
random_numbers.push_back( i+1 );
Regenerate();
}
void Regenerate()
{
std::random_shuffle(random_numbers.begin(), random_numbers.end());
}
int GetSize() const { return random_numbers.size(); }
int operator[](int i) const { return random_numbers; }
};

Tom.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top