+ 3
Why does rand() not appear to work
Ran the code for rand() several times - got the same answer each time. Does not seem very random to me. What is going on? Got the same sequence of numbers with the multiple production variant as well. Not looking good.
4 Réponses
+ 4
use srand() method to generate true random number
example :-
#include <iostream>
#include <ctime> // for time()
#include <cstdlib>// for srand() and rand()
using namespace std;
int main(){
srand(time(NULL));
cout<<rand();
}
+ 4
In your case a single seed is being used, so from that seed the same sequence of pseudorandom numbers is produced.
This is a key word too, pseudorandom, because the numbers are generated algorithmically, they just appear random to humans. A seed is used as a parameter to the algorithm so that you get different sequences. This could be provided by the user or taking the system date and time etc.
+ 1
By changing it's range we can change it's random pattern..but Still it's single seeded.