 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Graeme Guest
|
Posted: Tue Jul 29, 2003 2:52 am Post subject: srand() question |
|
|
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
|
|
| Back to top |
|
 |
David White Guest
|
Posted: Tue Jul 29, 2003 3:17 am Post subject: Re: srand() question |
|
|
Graeme <mtpletsch (AT) aol (DOT) com> wrote
| Quote: | 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.
| Quote: | 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.
| Quote: | 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
|
|
| Back to top |
|
 |
Yamin Guest
|
Posted: Tue Jul 29, 2003 5:00 am Post subject: Re: srand() question |
|
|
"Graeme" <mtpletsch (AT) aol (DOT) com> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
Bruce Guest
|
Posted: Tue Jul 29, 2003 5:28 am Post subject: Re: srand() question |
|
|
In comp.lang.c++
"Yamin" <absdfsd (AT) sdfdasfsd (DOT) com> wrote:
| Quote: | 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.
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Tue Jul 29, 2003 5:53 am Post subject: Re: srand() question |
|
|
"Graeme" <mtpletsch (AT) aol (DOT) com> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
David White Guest
|
Posted: Tue Jul 29, 2003 6:05 am Post subject: Re: srand() question |
|
|
Yamin <absdfsd (AT) sdfdasfsd (DOT) com> wrote
| Quote: |
"Graeme" <mtpletsch (AT) aol (DOT) com> wrote in message
news:1c162c57.0307281852.26dc01ce (AT) posting (DOT) google.com...
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
|
|
| Back to top |
|
 |
Karl Heinz Buchegger Guest
|
Posted: Tue Jul 29, 2003 2:18 pm Post subject: Re: srand() question |
|
|
Graeme wrote:
| Quote: |
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
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|