Velocity Reviews > C++ > random is not random enough?

# random is not random enough?

JNY
Guest
Posts: n/a

 12-29-2004
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?

Victor Bazarov
Guest
Posts: n/a

 12-29-2004
JNY wrote:
> 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

Ramspite
Guest
Posts: n/a

 12-29-2004
JNY wrote:

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

dandelion
Guest
Posts: n/a

 12-29-2004

"JNY" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed). ..
> 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.

KPB
Guest
Posts: n/a

 12-29-2004
Victor Bazarov wrote:
> JNY wrote:
>
>> 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

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

dandelion
Guest
Posts: n/a

 12-29-2004

"KPB" <(E-Mail Removed)> wrote in message news:VXzAd.24695\$(E-Mail Removed)...

<snip>

> 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 (<cstdlib>,
> <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...

velthuijsen@hotmail.com
Guest
Posts: n/a

 12-29-2004

JNY wrote:
> 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).

KPB
Guest
Posts: n/a

 12-29-2004
dandelion wrote:

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

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

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.

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

velthuijsen@hotmail.com
Guest
Posts: n/a

 12-29-2004

> Random is not a basic C++ function.

dangit that should have been
Random is not a basic C++ function IIRC.

(always do cya )

Victor Bazarov
Guest
Posts: n/a

 12-29-2004
KPB wrote:
> dandelion wrote:
>
>>
>> I know srand()/rand() is part of (<cstdlib>,
>>
>>> <cmath>... I forget which) but don't know about random().

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

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

I guess you didn't notice that the thread is cross-posted...

> [...]