Rand生成相同的数字 [英] Rand generating same numbers

查看:92
本文介绍了Rand生成相同的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我制作的小游戏有一个问题。

  #includestdafx.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<< 你需要猜测1和<< span<< endl;
int mynumber;
int numberofAttempts = 0;

do {
cout<< ++ numberofAttempts<<Attempt:;
cin>>我的号码;
if(mynumber> TheNumber)
cout<<Lower! << endl;
else if(mynumber< The Number)
cout<<Higher! << endl;
} while(mynumber!= TheNumber);

cout<< SUCESS !!! << endl;
return 0;
}

游戏应该生成一个0-100之间的随机数,应该猜测它。运行此代码后15-20次相同的数字生成了一些甚至8次(在我的情况下的数字2)。



我知道没有绝对的随机数,它使用一些数学公式或某事得到一个。我知道使用 srand(time(0))使它取决于当前时间。但我怎么会使它更的随机,因为我不想要的东西发生,我上面提到的。



我第一次运行它的结果是11 ,在再次运行之后(在猜出正确的数字之后),仍然是11,即使时间改变了。

解决方案

strong> [ADDITION1]



如果您真的希望研究更好的随机数生成,那么这是一个很好的算法: >

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



请记住,任何计算机生成的(即数学生成的)随机数只能是伪随机的。伪随机意味着当算法的输出看起来具有正态分布时,如果知道输入种子,它们是真正的确定性的。

[ORIGINAL]
请尝试以下几行之一:

  rand()%(span + 1); //这将给出0  -  100 
rand()%span; //这将给出0 - 99
rand()%span + 1; // This will give 1 - 100

而不是:

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

此外,不要将结果转换为double,然后放入int。



在这里也可以看到:



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



回应评论!
如果您使用:

  rand()/(span + 1); 

那么为了得到0和100之间的值,在0和(100 * 100)之间,这种性质必须得到保证。这是因为简单的划分。当rand()产生一个101 - 201时,值为1将会弹出,当rand()输出值为202 - 302等等时,一个2将弹出分区...



在这种情况下,你可能能够在100 * 100时消失,只有10000,并且在32位空间中肯定是大于这个整数,但是通常做一个除法不允许你利用提供的全数字空间!


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.

解决方案

[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!!!

这篇关于Rand生成相同的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆