473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Jul 19 '05 #1
6 10700
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

"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
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

"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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: David Shaw | last post by:
Hi, I'm writing a fun little program to play "Petals Around the Roses" with... I need to seed my random numbers so that they won't be the same every time I run the program, but my compiler won't...
1
by: Wavelet | last post by:
If I use srand(value) to change the random seed, when I close the vc or run the code from the beginning, the seeds of random will change to default value or still keep the value of what I designed...
1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
13
by: Jeremy Holdstadt | last post by:
This seems to be a C question to me. If it is not, I apologize. This command: awk 'BEGIN {srand();print srand()}' will give the number of seconds since the epoch, present time. Can any of you...
25
by: Merrill & Michele | last post by:
Many of us watched the World Series of Poker this last week and plotted how we were to take over that world. My current problem begins with shuffling the deck. For the apps I've written before,...
3
by: bobrics | last post by:
Hi, I am using srand() and would like to create different random numbers during a SINGLE execution of my program because I want to compare random cases. For now I have a switch statement within a...
2
by: Mara Guida | last post by:
"Each time I run my program, I get the same sequence of numbers back from rand()." The answer to question 13.17, in the c-faq is: #include <stdlib.h> #include <time.h> srand((unsigned...
12
by: Bill Cunningham | last post by:
I have read and studied and looked at references and can't find out how to do this. Here's where some real knowledge comes in now lets see who in clc really knows there stuff ;) main(void) {...
12
by: Lynn | last post by:
Excuse me. Is the sentence below a statement or a function; srand(time(NULL));
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.