Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > srand() question

Reply
Thread Tools

srand() question

 
 
Graeme
Guest
Posts: n/a
 
      07-29-2003
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
 
Reply With Quote
 
 
 
 
David White
Guest
Posts: n/a
 
      07-29-2003
Graeme <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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



 
Reply With Quote
 
 
 
 
Yamin
Guest
Posts: n/a
 
      07-29-2003

"Graeme" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Bruce
Guest
Posts: n/a
 
      07-29-2003
In comp.lang.c++
"Yamin" <(E-Mail Removed)> 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.

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-29-2003

"Graeme" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      07-29-2003
Yamin <(E-Mail Removed)> wrote in message
news:1cnVa.79357$vz%(E-Mail Removed) le.rogers.com...
>
> "Graeme" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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



 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-29-2003


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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments