Rand generates duplicate numbers
I have a problem with the little game I made.
#include "stdafx.h" #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int span = 100; srand(time(0)); int TheNumber = static_cast<double> (rand()) /RAND_MAX * (span -1) +1; cout << "You need to guess the number between 1 and " << span << endl; int mynumber; int numberofAttempts = 0; do { cout << ++numberofAttempts <<" Attempt: "; cin >> mynumber; if (mynumber > TheNumber) cout <<"Lower!" << endl; else if (mynumber < TheNumber) cout <<"Higher!" << endl; } while (mynumber != TheNumber); cout << "SUCESS!!!" << endl; return 0; } The game should generate a random number between 0-100, and you must guess it. After running this code 15-20 times, the same numbers were generated 8 more times (number 2 in my case).
I know that there is no absolute random number and that he uses some kind of mathematical formula or something to get it. I know that using srand(time(0)) makes it dependent on the current time. But how can I make this "more" random, since I do not want this to happen, as I mentioned above.
The first time I ran it, the result was 11, after restarting (after guessing the correct number) there were 11 more, although the time changed.
[ADDITION1]
If you really want to look at the best random number generation, then this is a good algorithm to start with:
http://en.wikipedia.org/wiki/Mersenne_twister
Remember that any "computer-generated" (i.e. mathematically generated) random number is ONLY a pseudo-random number. Pseudorandomly means that although the outputs from the algorithm have a normal distribution, they are really determinate if you know the input seed. True random numbers are not completely determined.
[ORIGINAL] Just try one of the following lines:
rand() % (span + 1); // This will give 0 - 100 rand() % span; // this will give 0 - 99 rand() % span + 1; // This will give 1 - 100 Instead:
(rand()) /RAND_MAX * (span -1) +1 Also, don't make the result of this double, and then put in an int.
See also:
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
In response to the comment !!! If you use:
rand() / (span + 1); then to get values ββbetween 0 and 100, the output from rand should really be between 0 and (100 * 100), and this character must be guaranteed. This is due to simple division. The value 1 essentially jumps out when rand () returns 101 - 201, and 2 jumps out of division when rand () returns 202 - 302, etc.
In this case, you can leave it at 100 * 100, this is only 10,000, and there are integers in the 32-bit space, large, but in the general case, the separation will not allow you to take advantage when using the full space number !!!
There are a number of issues with rand() . You are faced with one of them, which means that the first few values ββare not "random." If you should use rand() , it is always useful to discard the first four or the results from rand() .
srand (time(0)); rand(); rand(); rand(); rand(); Another problem with rand() is that low-order bits are not known to be random, even after the aforementioned hack. On some systems, low order bits alternate between 0,1,0,1,0,1, ... It is always better to use high order bits, for example, using a coefficient rather than a remainder.
Other problems: nonrandomness (most rand() implementations do not perform a series of random tests) and a short loop. For all these issues, it is best to use anything other than rand() .
First, rand() / RAND_MAX does not give a number from 0 to 1, it returns 0. This is because RAND_MAX matches 0 times as a result of rand (). Both are integers, so with integer division it does not return a floating point number.
Secondly, RAND_MAX can be the same size as INT. Multiplying RAND_MAX by something will lead to overflow.