# random is not random enough?

Discussion in 'C++' started by JNY, Dec 29, 2004.

1. ### JNYGuest

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?

JNY, Dec 29, 2004

2. ### Victor BazarovGuest

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

Victor Bazarov, Dec 29, 2004

3. ### RamspiteGuest

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.

Ramspite, Dec 29, 2004
4. ### dandelionGuest

"JNY" <> wrote in message
news:...
> 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.

dandelion, Dec 29, 2004
5. ### KPBGuest

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

KPB, Dec 29, 2004
6. ### dandelionGuest

"KPB" <> wrote in message news:VXzAd.24695\$...

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

dandelion, Dec 29, 2004
7. ### Guest

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

, Dec 29, 2004
8. ### KPBGuest

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.

KPB, Dec 29, 2004
9. ### Guest

> Random is not a basic C++ function.

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

(always do cya )

, Dec 29, 2004
10. ### Victor BazarovGuest

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

> [...]

Victor Bazarov, Dec 29, 2004
11. ### KPBGuest

Victor Bazarov wrote:
> 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...
>
>> [...]

Oops. Nope. That explains a lot.

Sorry to you C guys. I didn't mean to cross-post. This'll be my last one.

KPB, Dec 29, 2004
12. ### Michael MairGuest

KPB wrote:
> dandelion wrote:
>
>> KPB 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.

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
--
E-Mail: Mine is a gmx dot de address.

Michael Mair, Dec 29, 2004
13. ### KPBGuest

Michael Mair wrote:
> KPB wrote:
>
>> dandelion wrote:
>>
>>> KPB 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.

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

KPB, Dec 29, 2004
14. ### infobahnGuest

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.

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.

infobahn, Dec 29, 2004
15. ### KPBGuest

infobahn wrote:
> 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.

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

KPB, Dec 29, 2004
16. ### Michael MairGuest

KPB wrote:
> infobahn wrote:

[snip: KPB missed the fact that the thread is crossposted to both
c.l.c and c.l.c++]

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

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
--
E-Mail: Mine is a gmx dot de address.

Michael Mair, Dec 29, 2004
17. ### KPBGuest

Michael Mair wrote:

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

KPB, Dec 29, 2004
18. ### Gregory ToomeyGuest

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?

See Ramdom1 to Ramdom14 in

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

gtoomey

Gregory Toomey, Dec 29, 2004
19. ### Martin AmbuhlGuest

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?

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
matter what month September is in.

Martin Ambuhl, Dec 29, 2004
20. ### Robert B. ClarkGuest

On Wed, 29 Dec 2004 10:17:12 -0500, KPB <> wrote:

>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

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

--
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/

Robert B. Clark, Dec 29, 2004