srand() question

G

Graeme

I am writing a simple windows matching game with cards and pictures
(ie "concentration"). In order to write something like this, I need
to be able to give each "card" a random position in the assortment,
and I need the assortment to be different each time the program is
run. (I think) This means I need to be able to generate as many
random numbers at once as the number of "cards" in the assortment when
the program is first loaded. I have tried seeding the rand() function
with GetTickCount() as well as time():

srand(GetTickCount());

but the loop that generates the numbers is executed too fast, and I
end up getting 36 identical numbers. Is there a way to generate many
random numbers at once, and have them be different each time I run the
program? Possibly using recursion in the rand or srand functions?
Any help is greatly appreciated!

Thanks
Graeme
 
D

David White

Graeme said:
I am writing a simple windows matching game with cards and pictures
(ie "concentration"). In order to write something like this, I need
to be able to give each "card" a random position in the assortment,
and I need the assortment to be different each time the program is
run. (I think) This means I need to be able to generate as many
random numbers at once as the number of "cards" in the assortment when
the program is first loaded.

It sounds like the same as dealing cards at random from a deck for a card
game.
I have tried seeding the rand() function
with GetTickCount() as well as time():

srand(GetTickCount());

but the loop that generates the numbers is executed too fast, and I
end up getting 36 identical numbers.

This doesn't make sense to me. Each time you run the program the tick count
will be different, so if the rand() implementation is any good you should
get a different sequence of random numbers each time. The execution time of
the loop should be irrelevant, since it's only the single call to srand()
before the loop that determines the sequence returned by rand() inside the
loop. I would not expect the value returned by rand() to be affected by how
long ago it was last called.
Is there a way to generate many
random numbers at once, and have them be different each time I run the
program? Possibly using recursion in the rand or srand functions?

I'm sure you don't need anything as exotic as a recursive rand function,
whatever that would do. I think it's a much simpler problem than that.

DW
 
Y

Yamin

Graeme said:
I am writing a simple windows matching game with cards and pictures
(ie "concentration"). In order to write something like this, I need
to be able to give each "card" a random position in the assortment,
and I need the assortment to be different each time the program is
run. (I think) This means I need to be able to generate as many
random numbers at once as the number of "cards" in the assortment when
the program is first loaded. I have tried seeding the rand() function
with GetTickCount() as well as time():
[snip]

Hi Graeme,

might I suggest a simpler alternative.
Just keep getting random numbers until you get the number of random numbers
you need. Performance should be a non-issue in such an app. Ignore
repeated random numbers.

If you want want to shield from an infinite loop just incase rand() really
cannot get all the numbers:
prefill all numbers with numbers you generate manually, then rand() the
remaining. Say you only need to have 32 random numbers:
**********************************
//prefill the array with you initial values
std::vector<int> randNums;
randNums.resize(32);

srand(time() );
for( int i =0; i < 1024; i++)
{
int x = rand();
//do your scaling on x...i.e. x = x % 32

if ( ! in_randNums(x) ) //some function which checks if the number is
already in the array
{
randNums[ i % 32] = x;
}

}
**********************************


Yeah, its not the mose efficient, but it works.

Yamin
 
B

Bruce

In comp.lang.c++
Yamin said:
might I suggest a simpler alternative.
Just keep getting random numbers until you get the number of random numbers
you need. Performance should be a non-issue in such an app. Ignore
repeated random numbers.

See an algorithm in Programming Pearls by Jon Bentely about generating a
random set. It is very clever and will run in O(n) speed.
 
J

John Harrison

Graeme said:
I am writing a simple windows matching game with cards and pictures
(ie "concentration"). In order to write something like this, I need
to be able to give each "card" a random position in the assortment,
and I need the assortment to be different each time the program is
run. (I think) This means I need to be able to generate as many
random numbers at once as the number of "cards" in the assortment when
the program is first loaded. I have tried seeding the rand() function
with GetTickCount() as well as time():

srand(GetTickCount());

but the loop that generates the numbers is executed too fast, and I
end up getting 36 identical numbers. Is there a way to generate many
random numbers at once, and have them be different each time I run the
program? Possibly using recursion in the rand or srand functions?
Any help is greatly appreciated!

Thanks
Graeme

Use srand once at the beginning of your program, not every time you want a
random number.

john
 
D

David White

Yamin said:
Graeme said:
I am writing a simple windows matching game with cards and pictures
(ie "concentration"). In order to write something like this, I need
to be able to give each "card" a random position in the assortment,
and I need the assortment to be different each time the program is
run. (I think) This means I need to be able to generate as many
random numbers at once as the number of "cards" in the assortment when
the program is first loaded. I have tried seeding the rand() function
with GetTickCount() as well as time():
[snip]

Hi Graeme,

might I suggest a simpler alternative.
Just keep getting random numbers until you get the number of random numbers
you need. Performance should be a non-issue in such an app. Ignore
repeated random numbers.

Maybe your method would be okay in this case, but there is something
distasteful about an algorithm that gets slower and slower with each item
chosen. When all but one item has been chosen, it could take hundreds of
calls to rand() before it stumbles across the last one. There are some
pretty simple methods of doing this without any retries. You can have an
array of items and a loop that swaps every item with another, randomly
chosen, item. That guarantees that every item will be in a randomly chosen
position. Or you can choose a randomly chosen item on the fly, which the
following card-dealer does.

class CardDeck
{
enum { NCARDS = 52 };
enum { NSUITS = 4 };
public:
CardDeck()
{
// start with deck in order
for(int icard = 0; icard < NCARDS; ++icard)
{
deck[icard] = icard;
}
// make all cards available
cards_remaining = NCARDS;
}

void Shuffle()
{
cards_remaining = NCARDS;
}

int DealCard()
{
// return error if no cards left
if(cards_remaining == 0)
return -1;
int icard = rand() % cards_remaining;
int card = deck[icard];
deck[icard] = deck[cards_remaining-1];
deck[cards_remaining-1] = card;
--cards_remaining;
return card;
}

private:
int deck[NCARDS];
int cards_remaining;
};

To start with a fresh deck at any time, call Shuffle(). Then each successive
call to DealCard() will return a different card with only one rand() call
needed.

DW
 
K

Karl Heinz Buchegger

Graeme said:
I am writing a simple windows matching game with cards and pictures
(ie "concentration"). In order to write something like this, I need
to be able to give each "card" a random position in the assortment,
and I need the assortment to be different each time the program is
run. (I think) This means I need to be able to generate as many
random numbers at once as the number of "cards" in the assortment when
the program is first loaded. I have tried seeding the rand() function
with GetTickCount() as well as time():

srand(GetTickCount());

but the loop that generates the numbers is executed too fast, and I
end up getting 36 identical numbers.

I am pretty sure you made a common newbie mistake:

You called srand() every time before you called rand().

Don't do that! Call srand only once, eg. when your program starts
up.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top