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

> [...]

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post VK Javascript 15 05-02-2010 03:43 PM globalrev Python 4 04-20-2008 08:12 AM Lars-Erik Aabech ASP .Net 8 04-28-2005 07:52 AM Maziar Aflatoun ASP .Net 4 08-05-2004 01:26 AM Darren Clark ASP .Net 3 06-24-2004 05:23 PM