我的随机数只是第一次运行的随机数??? [英] My random number is only random for the first run???

查看:118
本文介绍了我的随机数只是第一次运行的随机数???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



#include< iostream>

#include< cstdlib>


//这个程序将是一个数字猜测游戏


int main()

{

//游戏变量

int number_of_tries = 0; //完成它需要多少次?

int current_guess = 0; //当前正在尝试的数字

int upper_limit = 100;

int lower_limit = 1;

int correct_number = rand()%100; //正确的数字将来自no

高于100

while(correct_number!= current_guess)

{

//用户互动

std :: cout<< ; 你的猜测是什么?正确的数字不会高于100,或低于1.。\ n" ;;

std :: cin>> current_guess; //用户输入他的猜测

if(current_guess> upper_limit || current_guess< lower_limit)

{

std :: cout << 什么,你不能读到指示?这个数字必须是

不高于100,或低于1!你被解雇了!!!!! \ n" ;;

std :: abort(); //如果输入不好就把用户踢出去

}

else if(current_guess< correct_number)

{

std :: cout<< 你的猜测太低了。再试一次'\\ nn ;;

number_of_tries ++;

}

else if(current_guess> correct_number)

{

std :: cout<< 你的猜测太高了。再试一次'\\ nn ;;

number_of_tries ++;

}

else if(current_guess == correct_number)

{

std :: cout<< 干得好!你猜对了正确的号码!!! \ n" ;;

std :: cout<< 它只花了你<< number_of_tries<< "尝试!\ n" ;;

休息;

}

}


返回0;

}

一切都很好。然后最奇怪的事情发生......

随机之后数字永远都是一样的。


#include<iostream>
#include<cstdlib>

//This program will be a number guessing game

int main()
{
//variables for the game
int number_of_tries=0;//how many times did it take to get it right?
int current_guess=0;//the number being currently tried
int upper_limit=100;
int lower_limit=1;
int correct_number=rand()%100;//the correct number will be from no
higher than 100
while (correct_number != current_guess)
{
//user interaction
std::cout << "What is your guess? The correct number will be no higher
than 100, or lower than 1.\n";
std::cin >> current_guess;//user inputs his guess
if (current_guess > upper_limit || current_guess < lower_limit)
{
std::cout << "What, you can''t read directions? The number has to be
no higher than 100, or lower than 1!!! You''re Fired!!!!!\n";
std::abort();//kicks user out if input is bad
}
else if (current_guess < correct_number)
{
std::cout << "Your guess is too low. Try again\n";
number_of_tries++;
}
else if (current_guess > correct_number)
{
std::cout << "Your guess is too high. Try again\n";
number_of_tries++;
}
else if (current_guess == correct_number)
{
std::cout << "Good job! You guessed the right number!!!\n";
std::cout << "It only took you "<< number_of_tries << " tries!\n";
break;
}
}

return 0;
}
All is good for the first run. Then the wierdest thing happens... The
"random" number is always the same forever afterwards.

推荐答案

xeys_00写道:
xeys_00 wrote:
所有对第一个都有好处跑。然后最奇怪的事情发生......
随机之后数字永远是一样的。
All is good for the first run. Then the wierdest thing happens... The
"random" number is always the same forever afterwards.



函数rand()生成伪随机数。每次运行程序时,你需要使用srand()

用不同的种子播种它,以便
生成不同的随机数序列。请参阅以下内容:

http: //cplus.about.com/od/cprogrammi.../aa041403b.htm


后来,

-

CrayzeeWulf


The function rand() generates pseudo-random numbers. You need to use srand()
to seed it with a different seed every time you run the program in order to
generate different sequences of random number. See the following:

http://cplus.about.com/od/cprogrammi.../aa041403b.htm

Later,
--
CrayzeeWulf


xeys_00写道:
xeys_00 wrote:
int correct_number = rand()%100; //正确的数字将来自no
高于100
int correct_number=rand()%100;//the correct number will be from no
higher than 100




你没有初始化随机种子。做一些类似


srand(time(NULL));


一开始你就会有更多随机的东西。随机数字(虽然

仍然保持高度确定性)。


虽然在这个非常必要的应用程序中,你应该

关注来自兰德的建议(3)如何避免在随机数字中使用低价位




问候,

约翰内斯


-

请核实我的签名。一些锻造巨魔声称是我。

我的GPG密钥ID是0xCC727E2E(日期为2004-11-03)。您可以从

wwwkeys.pgp.net或random.sks.keyserver.penguin.de获得它。

此外:来自Comcast Online的消息永远都是伪造的。


-----开始PGP签名-----

版本:GnuPG v1.4.1(GNU / Linux)

评论:将GnuPG与Thunderbird一起使用 - http://enigmail.mozdev.org


iD8DBQFCWGe5CseFG8xyfi4RAvMxAJsE / 6CeRJdHiBXgdOGyPmmhHX5JUgCfTu1z

wH + EA75o + BSr6p9RTKQVwps =

= xX7J

- ----结束PGP SIGNATURE -----



You''re not initializing the random seed. Do something like

srand(time(NULL));

at the very start and you''ll have more "random" random numbers (although
still remaining highly deterministic).

Although in this very application not really necessary, you should
follow the advice from rand(3) how to avoid using lower-order bits in
your random numbers.

Greetings,
Johannes

--
PLEASE verify my signature. Some forging troll is claiming to be me.
My GPG key id is 0xCC727E2E (dated 2004-11-03). You can get it from
wwwkeys.pgp.net or random.sks.keyserver.penguin.de.
Also: Messages from "Comcast Online" are ALWAYS forged.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCWGe5CseFG8xyfi4RAvMxAJsE/6CeRJdHiBXgdOGyPmmhHX5JUgCfTu1z
wH+EA75o+BSr6p9RTKQVwps=
=xX7J
-----END PGP SIGNATURE-----


Johannes Bauer写道:
Johannes Bauer wrote:
做类似的事情

srand(time(NULL));


srand(time(NULL))之间是否存在任何差异;和srand(时间(0));?


你应该按照兰德的建议(3)如何避免在
中使用低位随机数。
Do something like

srand(time(NULL));

Is there any difference at all between srand(time(NULL)); and srand(time(0));?

you should
follow the advice from rand(3) how to avoid using lower-order bits in
your random numbers.




这个建议在哪里,我如何得到它?


Thanx为你提供帮助,


wwwolf



Where is this advice, and how do I get it?

Thanx for your help,

wwwolf


这篇关于我的随机数只是第一次运行的随机数???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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