Rand generating same numbers

2019-02-26 04:16发布

问题:

I have a problem with the small game that 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 is supposed to generate a random number between 0-100 and you are supposed to guess it. After running this code 15-20times the same numbers generated some even 8 times (the number 2 in my case).

I know that there is no absolute random number and that it uses some math formula or something to get one.I know that using srand(time(0)) makes it dependent on the current time. But how would I make it "more" random, since I don't want the stuff to happen that I mentioned above.

First time I ran it the result was 11, after running it again (after guessing the right number) , it was still 11, even though the time changed.

回答1:

[ADDITION1]

If you DO truly wish to look into better random number generation, then this is a good algorithm to begin with:

http://en.wikipedia.org/wiki/Mersenne_twister

Remember though that any "Computer Generated" (i.e. mathematically generated) random number is ONLY pseudo-random. Pseudo-random means that while the outputs from the algorithm look to have normal distribution, they are truly deterministic if one knows the input seed. True random numbers are completely non-deterministic.

[ORIGINAL] Try simply 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 of:

(rand()) /RAND_MAX * (span -1) +1

Also, don't cast the result of that to a double, then place into an int.

Look here also:

http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

In Response to the comment!!! If you use:

rand() / (span + 1);

then in order to get values between 0 and 100, then the output values from rand would indeed have to be between 0 and (100 * 100), and this nature would have to be guaranteed. This is because of simple division. A value of 1 will essentially pop out when rand() produces a 101 - 201, a 2 will pop out of the division when the rand() outputs a value of 202 - 302, etc...

In this case, you may be able to get away with it at 100 * 100 is only 10000, and there are definitely integers larger than this in the 32 bit space, but in general doing a divide will not allow you to take advantage utilizing the full number space provided!!!



回答2:

There are a number of problems with rand(). You've run into one of them, which is that the first several values aren't "random". If you must use rand(), it is always a good idea to discard the first four or results from rand().

srand (time(0));
rand();
rand();
rand();
rand();

Another problem with rand() is that the low order bits are notoriously non-random, even after the above hack. On some systems, the lowest order bit alternates 0,1,0,1,0,1,... It's always better to use the high order bits such as by using the quotient rather than the remainder.

Other problems: Non-randomness (most implementations of rand() fails a number of tests of randomness) and short cycle. With all these problems, the best advice is to use anything but rand().



回答3:

First, rand() / RAND_MAX does not give a number between 0 and 1, it returns 0. This is because RAND_MAX fits 0 times in the result of rand(). Both are integers, so with integer division it does not return a floating point number.

Second, RAND_MAX may well be the same size as an INT. Multiplying RAND_MAX with anything will then give an overflow.



标签: c++ random