random is not random enough?

M

Martin Ambuhl

KPB said:
Yes. That I know. In this NG, I considered that info implicit.

Unfortunately, "this NG" is both comp.lang.c and comp.lang.c++, both of
which the OP posted to. That header files are C++ header files is never
implicit in comp.lang.c. In fact, they are off-topic.
I make my share, believe me but I don't think I made one here.

Only the one of failing to note which newsgroups you were posting to.
 
K

Keith Thompson

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, <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.)
 
P

Peter Ammon

JNY said:
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
 
J

Jesper Madsen

JNY said:
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
 
D

dandelion

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.
 
L

Lawrence Kirby

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top