0
Is it issue with Mobile ide.
I'm getting same output while running random number code. https://code.sololearn.com/ck0w9rSARcZO/?ref=app
3 ответов
+ 6
Pavithran Saminathan
Because the maximum return value by rand() is 32767 and this value is a library-defined identity. This value can be retrieved by making a query of the macro RAND_MAX as
cout << RAND_MAX << endl;
To extend the range of randomness and also having an acceptable distribution over that range, you'd probably want to use a more sophisticated library facilities like a range-based uniform distribution function.
For more info and example visit: [ https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution ]
P.S. To see more variation of the rand() put it inside a loop as
for (50 times) {
cout << rand() << endl;
}
+ 2
If you go through the tutorials, you'll be taught to seed the RNG algorithm using srand().
https://www.sololearn.com/learn/CPlusPlus/1638/?ref=app
In order to produce numbers which look random, the program will have to obtain values from a source which varies according to time. One of the most commonly used source, would be system time.
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
srand(time(0));
std::cout << rand();
}
0
Sir,Thanks for reply.
I got my answer.
This code is giving 5digit integer value.But only last 3 digit number keep changing everytime. should I add any function to change 5 digit number at everytime. is it possible to do.
https://code.sololearn.com/czV533T5Hxe6/?ref=app