int std::rand(): returns a pseudo-random integral value between 0 and RAND_MAX (both numbers included).
RAND_MAX (macro constant) is the maximum possible value generated by std::rand
std::srand() seeds the rand(). If rand() is used before any calls to std::srand( unsigned seed), rand() behaves as if it was seeded with std::srand(1) and each time rand() is seeded with std::srand(), it must produce the same sequence of values on successive calls. to generate random number between 0 and your upperbound just use rand() % upperbound.
It is important to only invoke the srand call ONCE at the beginning of the program ( before any calls to rand()). There is no need for repeat calls to seed the random number generator in fact, it will make your number less evenly distributed. The pseudo-random number generator should only be seeded once.
Standard practice is to use the result of a call to std::time(0) (means use current time as seed for random generator) as the seed. However, std::time returns a std::time_t value, and std::time_t is not guaranteed to be an integral type. In practice, though, every major implementation defines std::time_t to be an integral type, and this is also what POSIX requires.
Refs: 1
int upperbound=100;
std::srand(std::time(0)); //use current time as seed for random generator
//Will return an integer between [0,upperbound)
std::cout<<(rand() % upperbound) <<std::endl;int rangeFrom=0;
int rangeTo=1;
std::random_device rand_dev;
std::mt19937 generator(rand_dev());
std::uniform_real_distribution<double> distr(rangeFrom, rangeTo);
double rnd_number= distr(generator);uniform_real_distribution in the above can be replaced with:
bernoulli_distribution;
binomial_distribution;
exponential_distribution;
gamma_distribution;
geometric_distribution;
normal_distribution;
poisson_distribution;
cauchy_distribution;
chi_squared_distribution;
discrete_distribution;
extreme_value_distribution;
fisher_f_distribution;
lognormal_distribution;
negative_binomial_distribution;
piecewise_constant_distribution;
piecewise_linear_distribution;
student_t_distribution;
uniform_int_distribution;
uniform_real_distribution;
weibull_distribution;------------------------bernoulli distribution------------------------
0 *******************************************************************************************************************************************************************************************
1 **************************************************************
------------------------exponential distribution------------------------
0 **************************************************************************************************
1 ***********************************************************************************************
2 ***********************************
3 ************
4 ****
5 *
6
14
------------------------gamma distribution------------------------
0 *******************************************************
1 ****************************************************************************
2 **********************************************
3 ****************************
4 *****************
5 **********
6 ******
7 ***
8 **
9 *
10
11
------------------------normal distribution------------------------
-10
-9
-8
-7
-6
-5
-4
-3
-2 **
-1 ****
0 ********
1 *************
2 ********************
3 **************************
4 *******************************
5 *********************************
6 *******************************
7 **************************
8 ********************
9 *************
10 ********
11 ****
12 **
13
14
15
16
17
18
19
20
------------------------poisson distribution------------------------
0 ****
1 ******************
2 ************************************
3 ************************************************
4 ************************************************
5 ***************************************
6 *************************
7 **************
8 *******
9 ***
10 *
11
12
13