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?
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?
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 } mt19937 type?!" - A Mersenne Twister pseudo-random generator of 32-bit numbers with a state size of 19937 bits. It is a "wrapper" for the "mersenne_twister_engine" template (cplusplus.com/reference/random/mersenne_twister_engine) with pre-set params.std::uniform_int_distribution<> is inclusive -- [25,63] in the above example.-std=c++11 compiler flag. Since C++11 is not backwards compatible, all standard-complying compilers (GCC, clang,...) require C++11 to be activated explicitly.You can use the random functionality included within the additions to the standard library (TR1). Or you can use the same old technique that works in plain C:
25 + ( std::rand() % ( 63 - 25 + 1 ) ) std::rand() returns equally distributed integers from 0 to RAND_MAX (inclusively). Now, assuming you obtained integers in the range from 0 to RAND_MAX - 1 (inclusively) by using std::rand() % RAND_MAX, the chance of getting a 0 would now be doubled, since it will be the result when std::rand() returns either 0 or RAND_MAX. Any other number will only be obtained when directly returned by std::rand(). This will apply similarly, for every modulo that is not a divisor of RAND_MAX + 1.int range = max - min + 1; int num = rand() % range + min; rand() % range is relatively uniform, I would expect that adding a constant would preserve uniformity. Or are you saying that rand() itself isn't a great choice for a truly random number? That's true, but depending on what OP wants to do, rand() may be sufficient.Johannes Rudolph is complaining is that % does not work unless the value on the right is an exact divisor of RAND_MAX. If it is not then some values have a slight (very slight) smaller probability. example: RAND_MAX (32767) r = rand()%3; Probability of 0(10923/32767) 1(10922/32767) 2(10922/32767) Notice the probability of a 0 is slightly higher. As 'Benjamin Lindley' points out this is insignificant in most situations but when you do things like cryptography things like this become a problem. Thus a lot of people cry foul when this method is used.num = rand() * 1.0 / RAND_MAX * (max-min+1) + min;int random(int min, int max) //range : [min, max] { static bool first = true; if (first) { srand( time(NULL) ); //seeding for the first time only! first = false; } return min + rand() % (( max + 1 ) - min); } Use the rand function:
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
Quote:
A typical way to generate pseudo-random numbers in a determined range using rand is to use the modulo of the returned value by the range span and add the initial value of the range: ( value % 100 ) is in the range 0 to 99 ( value % 100 + 1 ) is in the range 1 to 100 ( value % 30 + 1985 ) is in the range 1985 to 2014 Benjamin Lindley answer above and my associated comments for the pre-C++11 approach. But my comment above was wrong (I miss read your answer). So I will delete it. The website cplusplus.com is OK bit I (and good for quick reference now and then) but I would NOT use it as an authoritative source.float RandomFloat(float min, float max) { float r = (float)rand() / (float)RAND_MAX; return min + r * (max - min); } max?