Increase the range rand () max

I created a test application that generates 10k random numbers ranging from 0 to 250,000. Then I calculated the MAX and min values ​​and noticed that the MAX value is always 32k ...

Do you have any ideas on expanding the range? I need a range with a MAX value of about 250,000!

+4
c ++
source share
5 answers

This is according to the definition of rand (), see:

http://cplusplus.com/reference/clibrary/cstdlib/rand/

http://cplusplus.com/reference/clibrary/cstdlib/RAND_MAX/

If you need larger random numbers, you can use an external library (e.g. http://www.boost.org/doc/libs/1_49_0/doc/html/boost_random.html ) or calculate large random numbers from several small random numbers.

But pay attention to the distribution you want to get. If you simply sum small random numbers, the result will not be evenly distributed.

If you simply scale one small random number by a constant factor, there will be gaps between the possible values.

Taking the product of random numbers also does not work.

A possible solution is as follows:

1) Take two random numbers a,b 2) Calculate a*(RAND_MAX+1)+b 

This way you get evenly distributed random values ​​up to (RAND_MAX + 1) ^ 2-1

+8
source share

Presumably, you also want equal distribution over this extended assortment. The only way you can do this effectively is to create a sequence of smaller numbers and scale them as if you were working on a different basis. For example, for 250,000, you can enter 4 random numbers in the range [0.10] and one in the range [0.25), along the lines:

 int random250000() { return randomInt(10) + 10 * randomInt(10) + 100 * randomInt(10) + 1000 * randomInt(10) + 10000 * randomInt(25); } 

For this to work, the random number generator must be good; many rand() implementations are not (or at least were not; recently checked the situation). You will also want to eliminate the bias that you get when matching RAND_MAX + 1 different values ​​to 10 or 25 different values. If RAND_MAX + 1 not an exact multiple of 10 and 25 (for example, it is an exact multiple of 50 ), you need something like:

 int randomInt( int upperLimit ) { int const limit = (RAND_MAX + 1) - (RAND_MAX + 1) % upperLimit; int result = rand(); while ( result >= limit ) { result = rand(); return result % upperLimit; } 

(Attention in this case: there are some machines where RAND_MAX + 1 will overflow; if portability is a problem, you will need additional precautions.)

All this, of course, assumes a generator of good quality, which is from the given.

+3
source share

You can simply manipulate your number bitwise to generate smaller random numbers.

For example, if you need a 32-bit random number:

 int32 x = 0; for (int i = 0; i < 4; ++i) { // 4 == 32/8 int8 tmp = 8bit_random_number_generator(); x <<= 8*i; x |= tmp; } 

If you don't need good randomness in your numbers, you can simply use rand () and 0xff for an 8-bit random number generator. Otherwise, you need something better.

+1
source share

Increase your numbers up by N / RAND_MAX , where N is your desired maximum. If the numbers fit, you can do something like this:

 unsigned long long int r = rand() * N / RAND_MAX; 

Obviously, if the initial part overflows, you cannot do this, but with N = 250000 you should be fine. RAND_MAX - 32K on many popular platforms.

In general, to get a random number evenly in the interval [A, B] , use:

 A + rand() * (B - A) / RAND_MAX; 

Of course, you should probably use the appropriate C ++ style <random> library; find this site for many similar questions, explaining how to use it.


Edit: In the hope of preventing the escalation of comments, here is another copy / paste of the correct C ++ solution for a really even distribution over the interval [A, B] :

 #include <random> typedef std::mt19937 rng_type; typedef unsigned long int int_type; // anything you like std::uniform_int_distribution<int_type> udist(A, B); rng_type rng; int main() { // seed rng first: rng_type::result_type const seedval = get_seed(); rng.seed(seedval); int_type random_number = udist(rng); // use random_number } 

Remember to leave RNG! If you keep the initial value, you can repeat the same random sequence later.

0
source share

Do you use short ints? If so, you will see 32,767 as your maximum number, because something more will overwhelm the short int.

0
source share

All Articles