random is not random enough?

J

JNY

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?
 
V

Victor Bazarov

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?

You are forgetting to seed the generator. RTFM on 'srand' function.

V
 
R

Ramspite

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?

Your missing the srand function. check you docs or google it for details.
 
D

dandelion

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?

Yes. Seed the random generator with some number (the number of seconds since
EPOCH, for instance).

See http://www.manpage.org/cgi-bin/man/man2html?query=srand
Is there another random number generator which I could try?

Many.
 
K

KPB

Victor said:
You are forgetting to seed the generator. RTFM on 'srand' function.

V


I don't mean to split hairs but isn't the function call supposed to be
rand() not random()?

What am I missing? I know srand()/rand() is part of (<cstdlib>,
<cmath>... I forget which) but don't know about random().

KPB
 
D

dandelion

I don't mean to split hairs but isn't the function call supposed to be
rand() not random()?
Ummm...

Right.

What am I missing?

From a quick glance in the standard, it appears that you
aren't missing anything.

I know srand()/rand() is part of ( said:
<cmath>... I forget which) but don't know about random().

<cstdlib> and <cmath> are C++ header files.

Phew... I'm not the only one who makes mistakes...
 
V

velthuijsen

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?

Random is not a basic C++ function. The ones you'd want to use are
srand and rand. because of this I'm going to assume a few things.
That is that under the hood this random calls srand once and then calls
rand after that.
If that is the case then your random does exactly what it is supposed
to do.
The rand function is a pseudo random generator. It starts from a seed
number (50 in this case) and based on that value returns you the next
value in a sequence (and uses the returned value to calculate the next
value of the sequence).
 
K

KPB

dandelion said:
<cstdlib> and <cmath> are C++ header files.

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

well said:
Phew... I'm not the only one who makes mistakes...

I make my share, believe me but I don't think I made one here.
 
M

Michael Mair

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

If I had said, it's in <random.h>, well, that's not a header file that's
part of the C++ standard library so it wouldn't have been applicable here.

The problem is, in _these_ NGs (c.l.c, c.l.c++), one half of the
participants does not (officially) know <cstdlib> and <cmath>...


Cheers
Michael
 
K

KPB

Michael said:
The problem is, in _these_ NGs (c.l.c, c.l.c++), one half of the
participants does not (officially) know <cstdlib> and <cmath>...


Cheers
Michael


Yes because I was an idiot and accidently cross-posted to the C group.
It wasn't my intention and it won't happen again.
 
I

infobahn

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

But "this NG" doesn't mean a lot when the thread is cross-posted.

In comp.lang.c, <cstdlib> and <cmath> are off-topic. In comp.lang.c++,
they are topical. The cross-posting is probably inappropriate.
 
K

KPB

infobahn said:
But "this NG" doesn't mean a lot when the thread is cross-posted.

In comp.lang.c, <cstdlib> and <cmath> are off-topic. In comp.lang.c++,
they are topical. The cross-posting is probably inappropriate.

Am I going to have to apologize to each and every one of you or will you
read on to the next posts in this thread first?
 
M

Michael Mair

KPB said:
infobahn wrote:
[snip: KPB missed the fact that the thread is crossposted to both
c.l.c and c.l.c++]
Am I going to have to apologize to each and every one of you or will you
read on to the next posts in this thread first?

Stop apologizing :)
The problem is that the usenet is asynchronous, so some of us
will not see the answers of others for some minutes, hours or
days after they have been sent.


Cheers
Michael
 
K

KPB

Michael said:
Stop apologizing :)
The problem is that the usenet is asynchronous, so some of us
will not see the answers of others for some minutes, hours or
days after they have been sent.

Ok. :)
 
G

Gregory Toomey

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?

See Ramdom1 to Ramdom14 in
http://remus.rutgers.edu/~rhoads/Code/code.html

You can use the time (perhaps to the millisecond) as a seed.

gtoomey
 
M

Martin Ambuhl

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?

1) 'random' is not the name of the standard library function for
generating pseudo-random numbers. That function is named 'rand'

2) If rand is used without first seeding with the srand function, it
will provide the same sequence on every run. This is a GoodThing(tm),
since it allows testing and debugging on the same sequence of values.

3) All this and more is covered in the FAQ
<http://www.eskimo.com/~scs/C-faq/top.html>, in particular see the
sections on random numbers, including
13.15 I need a random number generator.
13.16 How can I get random integers in a certain range?
13.17 Each time I run my program, I get the same sequence of numbers
back from rand().
13.18 I need a random true/false value, so I'm just taking rand() % 2,
but it's alternating 0, 1, 0, 1, 0...
13.20 How can I generate random numbers with a normal or Gaussian
distribution?

You will note that 13.17 is just the question that you asked. Always
check the FAQ before posting. Steve Summit has put in a lot of work
making sure that reliable answers are available to common questions.
This ensures that you don't have to put up with incorrect answers from
the clueless and that you don't suffer the anger of people who see the
same, already answered questions appearing here every September, no
matter what month September is in.
 
R

Robert B. Clark

Victor Bazarov wrote:

I don't mean to split hairs but isn't the function call supposed to be
rand() not random()?

random() is a non-standard function (peculiar to DOS implementations, if
not others) that returns a random number between 0 and num-1:

int random(int num)
What am I missing? I know srand()/rand() is part of (<cstdlib>,
<cmath>... I forget which) but don't know about random().

#include <stdlib.h>

As this post was cross-posted to comp.lang.c., let me add the obligatory
comment that <cstdlib> and <cmath> are not standard C header files, but
are in fact C++ headers.

The OP's code suggests C++, but since he cross-posted to c.l.c as well,
thought I'd mention it....
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top