# rand () - long numbers - HOW?

This is a discussion on rand () - long numbers - HOW? within the C Programming forums, part of the General Programming Boards category; Hi! How do I get random numbers between 1 and 1000000? I can't use rand () function beacuse it only ...

1. ## rand () - long numbers - HOW?

Hi!

How do I get random numbers between 1 and 1000000? I can't use rand () function beacuse it only returns int.

2. Could you get rand() to return a few random integers and add them together?

3. Thanks!

And no I don't use a 16 bit compiler. I'm using VC++ .NET compiler.

4. It's 0x7FFF = 32.767.

5. if you use a unix compiler w/ gcc, you get higher numbers for some reason, i guess it is their algorithm.

6. In his book C Traps and Pitfalls, Andrew Koenig states the following.
7.8 How big is a random number?

When the only C implementation ran on the PDP-11 computer, there was a function called rand that returned a (pseudo-) random nonegative integer. PDP-11 integers were 16 bits long, including the sign, so rand would return an integer between 0 and 215-1.

When C was implemented on the VAX-11, integers were 32 bits long. This raised an implementation question: what should be the range of the rand function on the VAX-11?

This question was answered differently in two parallel implementation efforts. When the people at the University of California at Berkeley did their C implementation, they took the view that rand should return a value that ranges over all possible nonegative integers, so their version of rand returns an integer between 0 and 231-1.

The people at AT&T, on the other hand decided that a PDP-11 program that expected the result of rand to be less than 215 would be easier to transport to a VAX-11 if the rand function returned a value between 0 and 215 there, too.

As a result it is now difficult to write a program that uses rand without tailoring it to the implementation. ANSI C defines a constant RAND_MAX equal to the largest random number, but eariler C implementations generally do not have it.
For the values 215 and 231, the small number is intended to simulate a superscript.