random number generator

A

Alan Lee

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

John Harrison

Alan Lee said:
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
 
D

David Harmon

On Tue, 6 Apr 2004 22:27:15 -0700 in comp.lang.c++, "Alan Lee"
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
 
J

John Harrison

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
 
A

Alan Lee

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

John Harrison

Alan Lee said:
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
 
D

David Harmon

On Tue, 6 Apr 2004 23:03:48 -0700 in comp.lang.c++, "Alan Lee"
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?
 
B

Bernhard Holzmayer

Alan said:
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
 
N

Niels Dybdahl

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
 
J

John Harrison

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
 
M

Marcin Kalicinski

U¿ytkownik "Alan Lee said:
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
 
P

Pete Becker

Alan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top