# random number generator

Discussion in 'C++' started by Alan Lee, Apr 7, 2004.

1. ### Alan LeeGuest

Is there any way to set RAND_MAX in rand()? I am looking for a way to get a
random number between 1 and 1E9. So far I can't figure out how to do that.
any suggestions would be greatly appreciated.

Alan Lee, Apr 7, 2004

2. ### John HarrisonGuest

"Alan Lee" <> wrote in message
news:c503fk\$p4j\$...
> Is there any way to set RAND_MAX in rand()? I am looking for a way to get

a
> random number between 1 and 1E9. So far I can't figure out how to do

that.
> any suggestions would be greatly appreciated.
>

No there is no way to set RAND_MAX.

There are two ways to go, you can either write a random number generator
from scratch. There must be tons of literature on the web on how to do this.

Or you have to call rand() several times to generate your large random
numbers.

On my system RAND_MAX is 0x7FFF which means that my random number generator
produces 16 bit random numbers. You need 1 to 1E9 which is at least 32 bits.
So if I call rand() twice (and I do it in the right way) I can produce a 32
bit random numbers.

The C FAQ has a few questions on random number generators (although not your
specific one). Have a look http://www.eskimo.com/~scs/C-faq/s13.html and
post back here if you are still stuck.

john

John Harrison, Apr 7, 2004

3. ### David HarmonGuest

On Tue, 6 Apr 2004 22:27:15 -0700 in comp.lang.c++, "Alan Lee"
<> wrote,
>Is there any way to set RAND_MAX in rand()?

No. RAND_MAX is there to tell _you_ what the behavior of rand() is.
You cannot change it.

> I am looking for a way to get a
>random number between 1 and 1E9. So far I can't figure out how to do that.
>any suggestions would be greatly appreciated.

Why? The answer to that will make a great difference in what it takes
to satisfy you. I guess your requirements greatly exceed rand()'s
promises. I suppose you need a professional grade random number
generator, perhaps from the cryptologists.

In any case, read the references mentioned in Q 13.5 of Steve Summit's C
FAQ. It is always good to check the FAQ before posting. You can get
the FAQ at:
http://www.eskimo.com/~scs/C-faq/top.html

David Harmon, Apr 7, 2004
4. ### John HarrisonGuest

>
> Or you have to call rand() several times to generate your large random
> numbers.
>

As David suggests, if you do this your random numbers will be of lower
quality.

john

John Harrison, Apr 7, 2004
5. ### Alan LeeGuest

For now, I don't care too much for the quality as long as its still a random
number generator. I will try to find a higher quality generator once I
finish my program but I just would like to test it for now. How do I get a
larger number by stringing calls to rand together?

"David Harmon" <> wrote in message
news:...
> On Tue, 6 Apr 2004 22:27:15 -0700 in comp.lang.c++, "Alan Lee"
> <> wrote,
> >Is there any way to set RAND_MAX in rand()?

>
> No. RAND_MAX is there to tell _you_ what the behavior of rand() is.
> You cannot change it.
>
> > I am looking for a way to get a
> >random number between 1 and 1E9. So far I can't figure out how to do

that.
> >any suggestions would be greatly appreciated.

>
> Why? The answer to that will make a great difference in what it takes
> to satisfy you. I guess your requirements greatly exceed rand()'s
> promises. I suppose you need a professional grade random number
> generator, perhaps from the cryptologists.
>
> In any case, read the references mentioned in Q 13.5 of Steve Summit's C
> FAQ. It is always good to check the FAQ before posting. You can get
> the FAQ at:
> http://www.eskimo.com/~scs/C-faq/top.html
>
>

Alan Lee, Apr 7, 2004
6. ### John HarrisonGuest

"Alan Lee" <> wrote in message
news:c505k5\$qoq\$...
> For now, I don't care too much for the quality as long as its still a

random
> number generator. I will try to find a higher quality generator once I
> finish my program but I just would like to test it for now. How do I get

a
> larger number by stringing calls to rand together?
>

For instance, to get a 32 bit random number from 2 16 bit random numbers

(rand() << 16)|rand()

This assumes that int is at least 32 bits, otherwise you'll have to add some
casts.

john

>
> "David Harmon" <> wrote in message
> news:...
> > On Tue, 6 Apr 2004 22:27:15 -0700 in comp.lang.c++, "Alan Lee"
> > <> wrote,
> > >Is there any way to set RAND_MAX in rand()?

> >
> > No. RAND_MAX is there to tell _you_ what the behavior of rand() is.
> > You cannot change it.
> >
> > > I am looking for a way to get a
> > >random number between 1 and 1E9. So far I can't figure out how to do

> that.
> > >any suggestions would be greatly appreciated.

> >
> > Why? The answer to that will make a great difference in what it takes
> > to satisfy you. I guess your requirements greatly exceed rand()'s
> > promises. I suppose you need a professional grade random number
> > generator, perhaps from the cryptologists.
> >
> > In any case, read the references mentioned in Q 13.5 of Steve Summit's C
> > FAQ. It is always good to check the FAQ before posting. You can get
> > the FAQ at:
> > http://www.eskimo.com/~scs/C-faq/top.html
> >
> >

>
>

John Harrison, Apr 7, 2004
7. ### David HarmonGuest

On Tue, 6 Apr 2004 23:03:48 -0700 in comp.lang.c++, "Alan Lee"
<> wrote,
>For now, I don't care too much for the quality as long as its still a random
>number generator. I will try to find a higher quality generator once I
>finish my program but I just would like to test it for now. How do I get a
>larger number by stringing calls to rand together?

Don't top post.

David Harmon, Apr 7, 2004
8. ### Bernhard HolzmayerGuest

Alan Lee wrote:

> For now, I don't care too much for the quality as long as its
> still a random
> number generator. I will try to find a higher quality generator
> once I
> finish my program but I just would like to test it for now. How
> do I get a larger number by stringing calls to rand together?
>

If you really don't care, why not do it like this:

rand()*(1e9/RAND_MAX)

Bernhard

Bernhard Holzmayer, Apr 7, 2004
9. ### Niels DybdahlGuest

> > How do I get a larger number by stringing calls to rand together?
> For instance, to get a 32 bit random number from 2 16 bit random numbers
> (rand() << 16)|rand()
> This assumes that int is at least 32 bits, otherwise you'll have to add

some
> casts.

If RAND_MAX is 0x7fff as in John Harrisons example, the formula above will
not work as wanted.
rand()*(RAND_MAX+1)+rand() is better in that case.

double n1=double(rand())/(RAND_MAX+1) will result in a number between 0 and
1 (1 not included).

The "range" can then be increased by:

double n2=(rand()+n1)/(RAND_MAX+1);

This can be repeated until the number of bits are achieved:

double n3=(rand()+n3)/(RAND_MAX+1);

This would give 45 bits if RAND_MAX is 15 bits.
Then finally you can map this into the range you want:

int r=int(n3*1e9)+1;

Niels Dybdahl

Niels Dybdahl, Apr 7, 2004
10. ### John HarrisonGuest

>
> If RAND_MAX is 0x7fff as in John Harrisons example, the formula above will
> not work as wanted.
> rand()*(RAND_MAX+1)+rand() is better in that case.
>

My brain can't have been functioning correctly when I posted that. But

rand()*(RAND_MAX+1)+rand()

is only 30 bit random number, assuming rand() is fifteen bits.

> How about using floating point:
>
> double n1=double(rand())/(RAND_MAX+1) will result in a number between 0

and
> 1 (1 not included).
>
> The "range" can then be increased by:
>
> double n2=(rand()+n1)/(RAND_MAX+1);

That doesn't seem right. Am I missing something?

john

John Harrison, Apr 7, 2004
11. ### Marcin KalicinskiGuest

U¿ytkownik "Alan Lee" <> napisa³ w wiadomo¶ci
news:c503fk\$p4j\$...
> Is there any way to set RAND_MAX in rand()? I am looking for a way to get

a
> random number between 1 and 1E9. So far I can't figure out how to do

that.
> any suggestions would be greatly appreciated.
>
>

Why don't use boost random?

Best regards,
Marcin

Marcin Kalicinski, Apr 7, 2004
12. ### Pete BeckerGuest

Alan Lee wrote:
>
> For now, I don't care too much for the quality as long as its still a random
> number generator. I will try to find a higher quality generator once I
> finish my program but I just would like to test it for now. How do I get a
> larger number by stringing calls to rand together?
>

You've gotten the basic advice on how to combine values from multiple
calls to rand. Don't let the naysayers influence you: even bad
implementations of rand are good enough for most applications. If you
determine (based on measurements, not on rumors) that the implementation
of rand that you're using isn't good enough for what you're doing, then
it's time to look at other generators.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Pete Becker, Apr 7, 2004