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?

Dani AI

Generated

For generating n integers in the range -10,000,000..10,000,000 in modern C++, prefer the <random> facility rather than C's rand(). RAND_MAX is commonly only 32767 and the low-order bits of rand() are weak, as pointed out. A practical, portable solution is an engine such as std::mt19937_64 paired with std::uniform_int_distribution; the distribution maps uniformly across the inclusive integer endpoints and avoids modulo or float-scaling bias.

#include <random>
#include <cstdint>

// seed once
std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_int_distribution<int32_t> dist(-10000000, 10000000);

// generate n values
for (std::size_t i = 0; i < n; ++i) {
    int32_t r = dist(gen);
    // r is in [-10000000, 10000000]
}

Notes and troubleshooting:

  • Seed the engine once and reuse it. Reseeding per sample kills randomness and is slow. For reproducible results, construct the engine with a fixed seed (e.g., std::mt19937_64 gen(12345);).
  • Prefer uniform_int_distribution for integer ranges; manual mappings like rand()%range or scaling floats introduce bias or rounding errors.
  • std::random_device may be non-deterministic on some platforms; for cryptographic needs use platform CSPRNGs or a crypto library rather than mt19937 engines.
  • In multithreaded programs, give each thread its own engine or synchronize access to a shared engine.
  • mt19937_64 has a very long period and is fast enough for most simulations and generation tasks.

This addresses ’s original range requirement while building on ’s warning about rand() and offering a concrete, modern approach for C++.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.