By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

srand() question

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Graeme <mt*******@aol.com> wrote in message
news:1c**************************@posting.google.c om...
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

Jul 19 '05 #2

P: n/a

"Graeme" <mt*******@aol.com> wrote in message
news:1c**************************@posting.google.c om...
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
Jul 19 '05 #3

P: n/a
In comp.lang.c++
"Yamin" <ab*****@sdfdasfsd.com> wrote:
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.

Jul 19 '05 #4

P: n/a

"Graeme" <mt*******@aol.com> wrote in message
news:1c**************************@posting.google.c om...
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
Jul 19 '05 #5

P: n/a
Yamin <ab*****@sdfdasfsd.com> wrote in message
news:1c*******************@news01.bloor.is.net.cab le.rogers.com...

"Graeme" <mt*******@aol.com> wrote in message
news:1c**************************@posting.google.c om...
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

Jul 19 '05 #6

P: n/a


Graeme wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.