Random number c++ in some range

Abdul Samad picture Abdul Samad · Sep 26, 2011 · Viewed 253.1k times · Source

Possible Duplicate:
Generate Random numbers uniformly over entire range

I want to generate the random number in c++ with in some range let say i want to have number between 25 and 63.

How can i have that.

Thanks

Answer

Cubbi picture Cubbi · Sep 26, 2011

Since nobody posted the modern C++ approach yet,

#include <iostream>
#include <random>
int main()
{
    std::random_device rd; // obtain a random number from hardware
    std::mt19937 gen(rd()); // seed the generator
    std::uniform_int_distribution<> distr(25, 63); // define the range

    for(int n=0; n<40; ++n)
        std::cout << distr(gen) << ' '; // generate numbers
}