随机数生成器

扫码查看
本文介绍了随机数生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好: 我正在尝试编写一个简单的程序,模拟向他们的出生日询问几个人 ,并计算有多少人被要求直到两个人有 同一天出生。我遇到的问题是第一个循环我得到一个 随机数序列,我得到一个匹配,然后在下面的 循环我得到了随机数SAME (?) 序列。我正在使用rand()。我不想使用随机数生成器过于花哨,但有没有办法让每次启动循环时停止rand()重置?还有什么其他 我有选择吗?我可以以某种方式使用系统时间来帮助我随机获得更多 吗?下面是到目前为止我的代码: 的#include<&的iostream GT; 的#include< cstdlib> 使用命名空间std; int main() { int const tests = 10000; int index; int i,j; int match = 0; int days [366] = {0}; int count = 0; for(j = 1; j< = tests; j ++) { while(match!= 1) { index =(rand()%365)+ 1; if(days [index]!= 1) { days [index] ++; count ++; } 其他 { for(i = 1; i< 366; i ++) { 天[i] = 0; } match = 1; } } cout<<计数<<结束; } 返回0; } 解决方案 是的,添加这样的一个电话: srand(时间(NULL)); 和你完成的工作。 请记住,这些仍然是伪随机数,但至少 种子将通过按< Enter>来改变。 [SNIP] 在循环开始时添加," srand(clock( )); 这个建议是一个关于随机性的坏主意。应该在一个程序中将srand称为 一次(!!)并且永远不要在循环内部。原因是 调用srand一次会产生一个起始种子,它定义了产生随机数的序列。在调用rand()后,内部种子会更新 ,这样下一个调用或rand()将导致下一个随机数 这个特定的序列。如果你更频繁地调用srand(),你将会从序列跳到序列,这肯定会破坏随机性。虽然 对于这种情况可能不是一个真正的问题你应该避免多次调用srand,除非你确切知道你在做什么和什么 结果是。 问候 克里斯 Hi all:I am trying to write a simple program that simulates asking several personstheir birth day and it counts how many persons are asked until two have thesame birth day. The problem that I have is that the first loop I get asequence of random numbers untuil I get a match, BUT then on the followingloops I get the SAME random(?) sequence. I am using rand(). I do not want toget too fancy with the random number generator, but is there a way ofstopping rand() from resetting every time it starts the loop? What otherchoices do I have? Can I somehow use the system time to help me get morerandom? Here is my code so far:#include <iostream>#include <cstdlib>using namespace std;int main(){int const tests = 10000;int index;int i,j;int match = 0;int days[366] = {0};int count = 0;for (j = 1; j <= tests; j++){while (match != 1){index = (rand() % 365) + 1;if (days[index] != 1){days[index]++;count++;}else{for (i = 1; i < 366; i++){days[i] = 0;}match = 1;}}cout << count << endl;}return 0;} 解决方案Yes, add ONE call like this:srand(time(NULL));and your done.Keep in mind that these are still pseudorandom numbers, but at least theseed will be changed by pressing the <Enter>.[SNIP] at the begining of the loop add, "srand(clock());"This advice is a bad, bad idea regarding randomness. srand should be calledonly once(!!) in a program and NEVER inside a loop. The reason is thatcalling srand once will yield in a start seed that defines the sequence ofrandom numbers produced. After calling rand() the internal seed is updatedso that the next call or rand() will result in the next random number ofthis specific sequence. If you call srand() more often you are going to jumpfrom sequence to sequence which will certainly destroy randomness. Althoughfor this case it might not be a real problem you should refrain frommultiple calls to srand, unless you know exactly what you are doing and whatthe results are.RegardsChris 这篇关于随机数生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-28 17:38
查看更多