473,503 Members | 5,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

random number geneator

Hi all:
I am trying to write a simple program that simulates asking several persons
their birth day and it counts how many persons are asked until two have the
same birth day. The problem that I have is that the first loop I get a
sequence of random numbers untuil I get a match, BUT then on the following
loops I get the SAME random(?) sequence. I am using rand(). I do not want to
get too fancy with the random number generator, but is there a way of
stopping rand() from resetting every time it starts the loop? What other
choices do I have? Can I somehow use the system time to help me get more
random? Here is my code so far:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int const tests = 10000;
int index;
int i,j;
int match = 0;
int days[366] = {0};
int count = 0;

for (j = 1; j <= tests; j++)
{
while (match != 1)
{
index = (rand() % 365) + 1;

if (days[index] != 1)
{
days[index]++;
count++;
}
else
{
for (i = 1; i < 366; i++)
{
days[i] = 0;
}

match = 1;
}

}

cout << count << endl;
}

return 0;
}
Jul 22 '05 #1
10 2871
In comp.lang.c++
"Sonoman" <so*****@aol.com> wrote:
get too fancy with the random number generator, but is there a way of
stopping rand() from resetting every time it starts the loop? What other
choices do I have? Can I somehow use the system time to help me get more
random?


Yes, add ONE call like this:

srand(time(NULL));

and your done.

Jul 22 '05 #2

----- Original Message -----
From: "Sonoman" <so*****@aol.com>
Newsgroups: comp.lang.c++
Sent: Wednesday, January 07, 2004 6:25 PM
Subject: random number geneator

Hi all:
I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I have is that the first loop I get a
sequence of random numbers untuil I get a match, BUT then on the following
loops I get the SAME random(?) sequence. I am using rand(). I do not want to get too fancy with the random number generator, but is there a way of
stopping rand() from resetting every time it starts the loop? What other
choices do I have? Can I somehow use the system time to help me get more
random? Here is my code so far:

#include <iostream>
#include <cstdlib>
add the header, <ctime>

using namespace std;

int main()
{
int const tests = 10000;
int index;
int i,j;
int match = 0;
int days[366] = {0};
int count = 0;

for (j = 1; j <= tests; j++)
{
while (match != 1)
at the begining of the loop add,
"srand(clock());"
{
index = (rand() % 365) + 1;

if (days[index] != 1)
{
days[index]++;
count++;
}
else
{
for (i = 1; i < 366; i++)
{
days[i] = 0;
}

match = 1;
}

} Here you need to put something to change the "seed" in the
"srand(clock());" statement, for example a counter that
requires <Enter> to be pressed every 10 loops
if(counter%10==9) cin.get();
cout << count << endl;
}
I think you wanted the last "}" bracket above "cout << count << endl;"
return 0;
}


Keep in mind that these are still pseudorandom numbers, but at least the
seed will be changed by pressing the <Enter>.
Jul 22 '05 #3

"Pierre Espenan" <wh**************@yahoo.com> wrote in message
news:zi*******************@newsread1.news.pas.eart hlink.net...

[SNIP]

for (j = 1; j <= tests; j++)
{
while (match != 1)


at the begining of the loop add,
"srand(clock());"


This advice is a bad, bad idea regarding randomness. srand should be called
only once(!!) in a program and NEVER inside a loop. The reason is that
calling srand once will yield in a start seed that defines the sequence of
random numbers produced. After calling rand() the internal seed is updated
so that the next call or rand() will result in the next random number of
this specific sequence. If you call srand() more often you are going to jump
from sequence to sequence which will certainly destroy randomness. Although
for this case it might not be a real problem you should refrain from
multiple calls to srand, unless you know exactly what you are doing and what
the results are.

Regards
Chris
Jul 22 '05 #4
On Thu, 08 Jan 2004 05:11:44 +0000, Bruce wrote:
In comp.lang.c++
"Sonoman" <so*****@aol.com> wrote:
get too fancy with the random number generator, but is there a way of
stopping rand() from resetting every time it starts the loop? What other
choices do I have? Can I somehow use the system time to help me get more
random?


Yes, add ONE call like this:

srand(time(NULL));

and your done.


Sound advice, but be aware that time() has low granularity. You may want
to mix in some (platform specific) other elements, like the PID of your
program, or hash the output from netstat and add that. If your program is
designed to run very shortly, this becomes important.

I once was bitten by repeated runs of a program giving exactly the same
results as time() changed only once a second and typically several runs of
the program could be completed in that one second.

Also be aware that many rand() implementations are very poor. For a
typical game this may be acceptable, but for cryptographic operations one
should use some better pseudo random generator (PRNG).

If you really need better random numbers, also have a look at EGD.

HTH,
M4

Jul 22 '05 #5
Chris Theis wrote:

"Pierre Espenan" <wh**************@yahoo.com> wrote in message
news:zi*******************@newsread1.news.pas.eart hlink.net...

[SNIP]

for (j = 1; j <= tests; j++)
{
while (match != 1)


at the begining of the loop add,
"srand(clock());"


This advice is a bad, bad idea regarding randomness. srand should be called
only once(!!) in a program and NEVER inside a loop. The reason is that
calling srand once will yield in a start seed that defines the sequence of
random numbers produced. After calling rand() the internal seed is updated
so that the next call or rand() will result in the next random number of
this specific sequence. If you call srand() more often you are going to jump
from sequence to sequence which will certainly destroy randomness. Although
for this case it might not be a real problem you should refrain from
multiple calls to srand, unless you know exactly what you are doing and what
the results are.


In addition, calling srand() only once allows you to replace
the "clock()" call with some constant so you can get REPEATABLE
sequences of random number for testing purposes. AFTER testing
is completed, restore the clock() call (or something similar).

Mike
Jul 22 '05 #6

"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:yv******************@news.chello.at...

"Pierre Espenan" <wh**************@yahoo.com> wrote in message
news:zi*******************@newsread1.news.pas.eart hlink.net...
[SNIP]

for (j = 1; j <= tests; j++)
{
while (match != 1)


at the begining of the loop add,
"srand(clock());"


This advice is a bad, bad idea regarding randomness. srand should be

called only once(!!) in a program and NEVER inside a loop. The reason is that
calling srand once will yield in a start seed that defines the sequence of
random numbers produced. After calling rand() the internal seed is updated
so that the next call or rand() will result in the next random number of
this specific sequence. If you call srand() more often you are going to jump from sequence to sequence which will certainly destroy randomness. Although for this case it might not be a real problem you should refrain from
multiple calls to srand, unless you know exactly what you are doing and what the results are.
Did you actually try to compile and run the code listed? Did you see what
he is talking about? The same number is generated by rand() in each loop
because the loop has a constant relationship to clock(). Puting in
cin.get() into the loop insures that a different seed is generated each
loop. I ran the modified program for 100 loops and the average for count
came out to 24 which is very close to the average expected by strict
mathematical analysis. The reason srand(clock()) is discouraged within a
loop is that unless special care is taken a new seed will not be generated.
Run the following code and see what happens...

#include <iostream>
#include <ctime>
#include <bitset>

using namespace std;

int main(){
bitset<365> date;
while(1){
srand(clock());
for(int count=1,i=0; ; ++count){
i=rand()%365;
if(date.test(i)) break;
date.flip(i);
}
cout << count << '\n';
date.reset();
cin.get();
}
return 0;
}


Regards
Chris



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Jul 22 '05 #7
Pierre Espenan wrote:

"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:yv******************@news.chello.at...

"Pierre Espenan" <wh**************@yahoo.com> wrote in message
news:zi*******************@newsread1.news.pas.eart hlink.net...
[SNIP]
>
> for (j = 1; j <= tests; j++)
> {
> while (match != 1)

at the begining of the loop add,
"srand(clock());"


This advice is a bad, bad idea regarding randomness. srand should be

called
only once(!!) in a program and NEVER inside a loop. The reason is that
calling srand once will yield in a start seed that defines the sequence of
random numbers produced. After calling rand() the internal seed is updated
so that the next call or rand() will result in the next random number of
this specific sequence. If you call srand() more often you are going to

jump
from sequence to sequence which will certainly destroy randomness.

Although
for this case it might not be a real problem you should refrain from
multiple calls to srand, unless you know exactly what you are doing and

what
the results are.


Did you actually try to compile and run the code listed?


Did you ever read the documentation for rand()?
If you call rand(), it will also update the seed value. Thus when calling
rand() the next time, it will generate a different random number (and
update the seed value again) such that when calling rand() the next time
a different random value is generated (and the seed is updated again) etc.

It is important to let rand() do this in its own, because randomness in
a computer is a statistical property. If you reseed the random number
generator to often, this statistical property cannot build up (as with
all statistics, it works only for a sufficient large number of samples,
in this case a sufficient large number of random values).
Did you see what
he is talking about? The same number is generated by rand() in each loop
because the loop has a constant relationship to clock().
It doesn't matter. The main problem is, that srand() should be called only
once (!) in a program, not often, especially not before every call to rand()!
BTW: The original code didn't have a call to srand() at all. Thus there could
not have been any realtionship to clock() (which was also not in the original
code).

Common practice is to not use clock() for seeding the random number generator
but using time() to do that.
Puting in
cin.get() into the loop insures that a different seed is generated each
loop.
And how does this help in a program which should run on it's own without
a user?
I ran the modified program for 100 loops and the average for count
came out to 24 which is very close to the average expected by strict
mathematical analysis. The reason srand(clock()) is discouraged within a
loop is that unless special care is taken a new seed will not be generated.


The reason why you shouldn't call srand() more then once is, that you destroy
the randomness of the sequence if you do it. A random number generator is
designed to give a *sequence* of random numbers. It is only the sequence
which shows the properties of randomness. Don't interfere with the
generation of the sequence or you will destroy randomness very easily.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...
Pierre Espenan wrote:

"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:yv******************@news.chello.at...

"Pierre Espenan" <wh**************@yahoo.com> wrote in message
news:zi*******************@newsread1.news.pas.eart hlink.net...
>
[SNIP]
> >
> > for (j = 1; j <= tests; j++)
> > {
> > while (match != 1)
>
> at the begining of the loop add,
> "srand(clock());"

This advice is a bad, bad idea regarding randomness. srand should be called
only once(!!) in a program and NEVER inside a loop. The reason is that
calling srand once will yield in a start seed that defines the sequence of random numbers produced. After calling rand() the internal seed is updated so that the next call or rand() will result in the next random number of this specific sequence. If you call srand() more often you are going
to jump
from sequence to sequence which will certainly destroy randomness.

Although
for this case it might not be a real problem you should refrain from
multiple calls to srand, unless you know exactly what you are doing
and what
the results are.


Did you actually try to compile and run the code listed?


Did you ever read the documentation for rand()?
If you call rand(), it will also update the seed value. Thus when calling
rand() the next time, it will generate a different random number (and
update the seed value again) such that when calling rand() the next time
a different random value is generated (and the seed is updated again) etc.

It is important to let rand() do this in its own, because randomness in
a computer is a statistical property. If you reseed the random number
generator to often, this statistical property cannot build up (as with
all statistics, it works only for a sufficient large number of samples,
in this case a sufficient large number of random values).
Did you see what
he is talking about? The same number is generated by rand() in each loop because the loop has a constant relationship to clock().


It doesn't matter. The main problem is, that srand() should be called only
once (!) in a program, not often, especially not before every call to

rand()! BTW: The original code didn't have a call to srand() at all. Thus there could not have been any realtionship to clock() (which was also not in the original code).

Common practice is to not use clock() for seeding the random number generator but using time() to do that.
Oops! I tried srand(time(NULL)) outside of the loop and this does yield the
correct result without cin.get() so I guess this is the source of all the
problems
generating (pseudo) random numbers I have been having. I just confused
clock()
with time().
Puting in
cin.get() into the loop insures that a different seed is generated each
loop.
And how does this help in a program which should run on it's own without
a user?
I ran the modified program for 100 loops and the average for count
came out to 24 which is very close to the average expected by strict
mathematical analysis. The reason srand(clock()) is discouraged within a loop is that unless special care is taken a new seed will not be

generated.
The reason why you shouldn't call srand() more then once is, that you destroy the randomness of the sequence if you do it. A random number generator is
designed to give a *sequence* of random numbers. It is only the sequence
which shows the properties of randomness. Don't interfere with the
generation of the sequence or you will destroy randomness very easily.

--
Karl Heinz Buchegger
kb******@gascad.at



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Jul 22 '05 #9
First of all, Thanks for the help. The srand works better outside the loop.
The first time I ran the program I got a sequence of random numbers. BUT on
the second time and the times after that, when I execute the program I get
the same exact sequence of numbers. Does anyone care to explain why? Any
simple solutions out there? Any clever solutions for my print statement? It
just looks to stupid. Here is my current code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
int index;
int i;
double j;
int match = 0;
int days[366] = {0};
double count = 0;
double sum = 0;
double average;

srand(clock());

for (j = 1; j <= 10000; j++)
{
while (match != 1)
{
index = (rand() % 365) + 1;

if (days[index] != 1)
{
days[index]++;
count++;
}
else
{
for (i = 0; i < 366; i++)
{
days[i] = 0;
}

match = 1;
sum = sum + count;
average = sum/j;
}
}

if( j == 1 || j == 2 || j == 3 || j == 4 || j == 5 ||
j == 10 || j == 25 || j == 50 || j == 100 ||
j == 1000 || j == 10000)
{
cout << "test # " << j << " asked " << count <<
" persons, the average so far is: " << average << endl;
}

match = 0;
count = 0;
}

return 0;
}
Jul 22 '05 #10
On Fri, 09 Jan 2004 17:21:36 -0500, Sonoman wrote:
First of all, Thanks for the help. The srand works better outside the loop.
The first time I ran the program I got a sequence of random numbers. BUT on
the second time and the times after that, when I execute the program I get
the same exact sequence of numbers. Does anyone care to explain why? Any
simple solutions out there? Any clever solutions for my print statement? It
just looks to stupid. Here is my current code:

srand(clock());


CLOCK(3) Linux Programmer's Manual CLOCK(3)

NAME
clock - Determine processor time

SYNOPSIS
#include <time.h>

clock_t clock(void);

DESCRIPTION
The clock() function returns an approximation of processor time used by
the program.

Clock() always returns (aproximately) the same number. Use time() instead.

HTH,
M4

Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2482
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
4
2682
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
70
6173
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
10
5948
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object...
16
12051
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
5
3324
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
4
10537
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
13
2768
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
2
5455
by: alishaikhji | last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.