C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: random is not random enough?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Keith Thompson
Guest





PostPosted: Wed Dec 29, 2004 10:19 pm    Post subject: Re: random is not random enough? Reply with quote



[email]jny0 (AT) hotmail (DOT) com[/email] (JNY) writes:
Quote:
I am using random to generate random numbers, thus:

int x,y;

for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}

When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?

Cross-posting to comp.lang.c and comp.lang.c++ was probably not a good
idea. "cout << x;" is of course specific to C++.

random() is not a standard C function. There is a POSIX function
called random(); your system should have documentation for it.
"man random" if you're on a Unix-like system.

The C standard function for generating random numbers is called
rand(). It should also be documented on your system, and section 13
of the C FAQ, good information. (Many implementations of rand() aren't very good;
using a non-standard function like random() might be a good idea.)

--
Keith Thompson (The_Other_Keith) [email]kst-u (AT) mib (DOT) org[/email] <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Back to top
Peter Ammon
Guest





PostPosted: Wed Dec 29, 2004 10:43 pm    Post subject: Re: random is not random enough? Reply with quote



JNY wrote:
Quote:
I am using random to generate random numbers, thus:

int x,y;

for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}

When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?

Note that if the program takes less than one second to execute and uses
time() as a seed, rerunning it may produce the same sequence, since
time() has one second resolutions on many implementations.

-Peter

--
Pull out a splinter to reply.

Back to top
Jesper Madsen
Guest





PostPosted: Wed Dec 29, 2004 11:31 pm    Post subject: Re: random is not random enough? Reply with quote




"JNY" <jny0 (AT) hotmail (DOT) com> wrote

Quote:
I am using random to generate random numbers, thus:

int x,y;

for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}

When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?

I do not know the random() function, but if you need another random
function, look at the Mersenne Twister... It is very good...

http://www.math.keio.ac.jp/matumoto/emt.html

There are both C and C++ versions around



Back to top
dandelion
Guest





PostPosted: Thu Dec 30, 2004 9:45 am    Post subject: Re: random is not random enough? Reply with quote


"KPB" <k@w.net> wrote


<snip>

Quote:
Yes because I was an idiot and accidently cross-posted to the C group.


<snip>

Don't beat yourself up. By that standard, almost everyone on both ng's is an
idiot. With li'l'ol' me right at the top of the list.




Back to top
Lawrence Kirby
Guest





PostPosted: Thu Dec 30, 2004 11:42 am    Post subject: Re: random is not random enough? Reply with quote

On Wed, 29 Dec 2004 22:19:55 +0000, Keith Thompson wrote:

....

Quote:
random() is not a standard C function. There is a POSIX function
called random(); your system should have documentation for it.
"man random" if you're on a Unix-like system.

My Linux man page says that it is a BSD function, AFAIK it isn't POSIX. It
apparently has a related seeding function called srandom(). The standard C
library (which of course is also available to C++ programs) provides
rand() and srand(). That's a good starting point. Don't try to mix the two
different sets of functions.

To the OP: these are examples of pseudo-random number generators. The
numbers are generated algorithmically, and the algorithms used are
designed to generate sequences that *appear* random and which fare well
for statistical tests for randomness (at least the better algorithms do).
However they aren't random in the true sense, because as you've seen the
sequences are reproducible. If you start in a particular state you will
always end up with the same sequence of numbers. This is why it is
important to "seed" the pseudo-random number generator if you want
different sequences each time.

Quote:
The C standard function for generating random numbers is called rand().
It should also be documented on your system, and section 13 of the C
FAQ, <http://www.eskimo.com/~scs/C-faq/top.html>, has some good
information. (Many implementations of rand() aren't very good; using a
non-standard function like random() might be a good idea.)

Alternatively include code for the RNG in your program. Good
implementations are written in standard C (or C++ for our fellow readers)
and using such code will not tie your program to a particular system.

Lawrence


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.