Velocity Reviews > C++ > random number generator

# random number generator

Alan Lee
Guest
Posts: n/a

 04-07-2004
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.

John Harrison
Guest
Posts: n/a

 04-07-2004

"Alan Lee" <(E-Mail Removed)> wrote in message
news:c503fk\$p4j\$(E-Mail Removed)...
> 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

David Harmon
Guest
Posts: n/a

 04-07-2004
On Tue, 6 Apr 2004 22:27:15 -0700 in comp.lang.c++, "Alan Lee"
<(E-Mail Removed)> 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
Guest
Posts: n/a

 04-07-2004
>
> 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

Alan Lee
Guest
Posts: n/a

 04-07-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Tue, 6 Apr 2004 22:27:15 -0700 in comp.lang.c++, "Alan Lee"
> <(E-Mail Removed)> 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
Guest
Posts: n/a

 04-07-2004

"Alan Lee" <(E-Mail Removed)> wrote in message
news:c505k5\$qoq\$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > On Tue, 6 Apr 2004 22:27:15 -0700 in comp.lang.c++, "Alan Lee"
> > <(E-Mail Removed)> 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
Guest
Posts: n/a

 04-07-2004
On Tue, 6 Apr 2004 23:03:48 -0700 in comp.lang.c++, "Alan Lee"
<(E-Mail Removed)> 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.

Follow the advice you just read in the FAQ I pointed you at. You did
read it, didn't you?

Bernhard Holzmayer
Guest
Posts: n/a

 04-07-2004
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

Niels Dybdahl
Guest
Posts: n/a

 04-07-2004
> > 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.

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

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

John Harrison
Guest
Posts: n/a

 04-07-2004
>
> 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

 Thread Tools

 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 Kingsley Oteng VHDL 11 08-09-2010 10:00 AM VK Javascript 15 05-02-2010 03:43 PM globalrev Python 4 04-20-2008 08:12 AM Wally ASP .Net 1 03-20-2006 12:19 AM tshad ASP .Net 3 05-06-2005 06:15 PM

Advertisments