Question 01:
Is there any technique where generating random number within a range is equiprobable ?
Question 02:
What is the extra advantage of the following method 02 and method 03 ?
srand(time(NULL);
//Method 01: general approach
int myrand(int mod){
return rand()%mod;
}
//Method 02: Taken form red coder submission.
int myrand(int mod) {
int t = rand() % mod;
t = (1LL*t*RAND_MAX + rand()) % mod;
return t;
}
//Method 03: Also Taken from red coder submission.
int myrand(int mod) {
return (int) ( (((double) rand() / RAND_MAX) * (mod) ));
}
Updated : Idea from diko.
auto seed = auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
std::mt19937 mt(seed);
int myrand(int mod) {
return mt()%mod;
}