I am required to generate "n" random numbers of which each random number ranges from -10,000,000 to 10,000,000. I tried using rand() and changing the range, but it only has a max range of 32,000 or so. What is a good function or algorithm that I could use to achieve this and what library would I have to include in my code?

Edited by .:Pudge:.: n/a

6 Years
Discussion Span
Last Post by StuXYZ

Well first of if you need random numbers to actually compute something else, don't use rand() for anything, it simple isn't random enough. [Basically, rand() has very poor lower bits.] e.g. see http://www.eternallyconfuzzled.com/arts/jsw_art_rand.aspx

So that leads us to looking for a good [enough] random number generator. The current defacto standard for most scientific simulations is currently MesenneTwister [e.g. GEANT, gsl, octave]. Mersene-Twister can be found at http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html. Note, this site links to the original Mersenne Twister, and some of the faster (but same randomness) implementations that exist. All of the links go to open source code. It also links to the scientific papers about the random number generator, be warned heavy maths.

Finally, what you are after is to create a random number between to numbers (considered a range). Using your favorite random number generator, you are typically going to get a random number between 0 and 1. If you are using Mersenne Twister you have a choice of getting the number fully inclusive, partly inclusive and totally exclusive [i.e. you can/cannot get the extreme values one and zero].
You have to be careful about that choice and its effect on your applications, then you want to convert it into your range. This is done like this:

double rV= lowerNumber+ (higherNumber-lowerNumber)*random();

Note that you might have to cast the number to an integer, again be careful to ensure that you can/can't get the lowest/highest numbers in the range.

Edited by StuXYZ: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.