C++ 为什么掷骰子的结果如此平均?

C++ 为什么掷骰子的结果如此平均?,c++,C++,这应该是掷6个骰子,并记录有多少是唯一的。例如:123456=6个唯一数字,11111=1个唯一数字,12333=3个唯一数字。我每次掷骰子都会得到一个非常平均的百分比,大约16.7% #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int numberGenerator() { int x = (rand() % 6) + 1; return x; }

这应该是掷6个骰子,并记录有多少是唯一的。例如:123456=6个唯一数字,11111=1个唯一数字,12333=3个唯一数字。我每次掷骰子都会得到一个非常平均的百分比,大约16.7%

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int numberGenerator()
{
int x = (rand() % 6) + 1;
return x;
}

int diceCounter()
{

int counter[6] = {0,0,0,0,0,0};

for (int i = 0; i < 6; i++)
    {
    int k = numberGenerator();
        if (k == 1)
           counter[0] = 1;
        if (k == 2)
           counter[1] = 1;
        if (k == 3)
           counter[2] = 1;
        if (k == 4)
           counter[3] = 1;
        if (k == 5)
           counter[4] = 1;
        if (k == 6)
           counter[5] = 1;
     }
return counter[0]+counter[1]+counter[2]+counter[3]+counter[4]+counter[5];
}                 // returns how many unique numbers were rolled


int main()
{
srand(time(NULL));
cout << diceCounter() << endl;

int a1 = 0;
int a2 = 0;
int a3 = 0;
int a4 = 0;
int a5 = 0;
int a6 = 0;
int p;


for (int j = 0; j < 1000000; j++)
{
p = numberGenerator();        // for number of unique numbers, it adds +1 to counter
if (p == 1)
a1 += 1;
if (p == 2)
a2 += 1;
if (p == 3)
a3 += 1;
if (p == 4)
a4 += 1;
if (p == 5)
a5 += 1;
if (p == 6)
a6 += 1;
}
cout << "1 ==> " << (a1 / 1000000.0)*100 << "%" << endl;
cout << "2 ==> " << (a2 / 1000000.0)*100 << "%" << endl;
cout << "3 ==> " << (a3 / 1000000.0)*100 << "%" << endl;
cout << "4 ==> " << (a4 / 1000000.0)*100 << "%" << endl;
cout << "5 ==> " << (a5 / 1000000.0)*100 << "%" << endl;
cout << "6 ==> " << (a6 / 1000000.0)*100 << "%" << endl;

                // this results in uniform 16.7% percentages
}
#包括
#包括
#包括
使用名称空间std;
int numberGenerator()
{
int x=(rand()%6)+1;
返回x;
}
整数计数器()
{
int计数器[6]={0,0,0,0,0};
对于(int i=0;i<6;i++)
{
int k=数字发电机();
如果(k==1)
计数器[0]=1;
如果(k==2)
计数器[1]=1;
如果(k==3)
计数器[2]=1;
如果(k==4)
计数器[3]=1;
如果(k==5)
计数器[4]=1;
如果(k==6)
计数器[5]=1;
}
返回计数器[0]+计数器[1]+计数器[2]+计数器[3]+计数器[4]+计数器[5];
}//返回滚动的唯一数字的数量
int main()
{
srand(时间(空));

cout在您的循环中,您正在调用
numberGenerator
而不是
diceCounter


因此,不是计算掷6个骰子的唯一结果的数量,而是计算单个骰子的每个数字的数量。正如您所预期的,每个数字都会在1/6的时间内出现。

在循环中,您调用的是
numberGenerator
,而不是
骰子计数器


因此,不是计算掷6个骰子的唯一结果的数量,而是计算单个骰子的每个数字的数量。正如您所预期的,每个数字的出现时间为1/6。

增加了@MichaelAnderson的答案:

随机数实际上都是伪随机的

它们处理
seed
值。因此,每次调用
rand()
,在此之前调用
srand()
,以重新设置随机数生成器的种子: 像这样:


对@MichaelAnderson的回答补充如下:

随机数实际上都是伪随机的

它们处理
seed
值。因此,每次调用
rand()
,在此之前调用
srand()
,以重新设置随机数生成器的种子: 像这样:


大数定律?但1或6个唯一数的百分比应该比3或4大数定律低很多?但1或6个唯一数的百分比应该比3或4小很多。你只需要在程序开始时这样做一次,并且只有当你希望程序每次运行都产生不同的随机数时顺序。如果在每次调用
rand()
之前执行此操作,并且循环时间不到1秒,则每次都会得到相同的随机数。如果查看main的第一行,它有一个种子调用(
srand
)在这本书中——这是我寻找的第一件事……好吧,重新播种并没有什么坏处——我想@Michaelandersonally正如@Barmar指出的那样,重新播种每个兰德电话都是非常糟糕的—(a)它很贵,(b)如果循环占用时间,则只需在程序开始时执行一次,并且只需在每次运行程序时生成不同的随机序列。如果在每次
rand()之前执行此操作
调用,并且循环时间不到1秒,每次都会得到相同的随机数。如果查看main的第一行,它有一个种子调用(
srand
)在这本书中——这是我寻找的第一件事……嗯,重新播种并没有什么坏处——我想@Michaelandersonally正如@Barmar所指出的那样,重新播种每个rand通话都是非常糟糕的—(a)它很贵,(b)如果你的循环需要的话
srand(time(NULL));
int r = rand();