主题:c++中还有比rand()更好些的随机数产生器吗?
tuanlianshi
[专家分:0] 发布于 2011-04-14 20:14:00
如下程序产生1-10之间的均匀分布随机数,每组十个随机数,产生多少组自定,然后统计1,2,3,4,5,6,7,8,9,10分别出现的次数. 使用了c++自带的rand()函数来产生, 如果随机数
产生器绝对完美,则每个数字出现次数应该相等. 实际上不可能都相等,总有误差.
但测试结果误差似乎比较大, 当产生十万组数字(共有一百万个随机数)时,误差才被降至百分
之一以下, 当产生一千万组数字(共有一亿个随机数)时,误差才被降至千分之一以下.
(测试结果见程序后)
不知道还有更好的随机数产生器吗? 恳请高手赐教, 感激不尽!
//This is a program to test making random number, using the calendar time as the random seed
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
int random_number , count[10], sim_times;
char letter;
time_t seed ;
seed = time(NULL);
srand(seed);
ofstream out_file;
out_file.open("result.txt");
do{
cout<<"Please enter the simulation times : ";
out_file<<"Please enter the simulation times : ";
cin>>sim_times;
out_file<<sim_times<<endl;
for(int k=0; k<=9; k++)
count[k]=0 ;
for(int j=1; j<=sim_times; j++)
{
// cout<<"team "<<j<<" :" ;
for(int i=1; i<=10; i++)
{
random_number = rand()%10 + 1;
count[random_number-1]++ ;
// cout<<" "<<random_number ;
}
// cout<<endl;
}
for(int k=0; k<=9; k++)
{ cout<<"Number "<<k+1<<" emerges "<<count[k]<<" times,"
<<" error rate is "<<fabs(1.0*(count[k]- sim_times))/sim_times<<endl;
out_file<<"Number "<<k+1<<" emerges "<<count[k]<<" times,"
<<" error rate is "<<fabs(1.0*(count[k]- sim_times))/sim_times<<endl;
}
cout<<"Do you want another simulation ? ";
out_file<<"Do you want another simulation ? ";
cin>>letter;
out_file<<letter;
cout<<endl;
out_file<<endl<<endl;
}while(letter=='Y' || letter=='y');
system("pause");
return 0 ;
}
回复列表 (共6个回复)
沙发
tuanlianshi [专家分:0] 发布于 2011-04-14 20:16:00
以下是测试结果
Please enter the simulation times : 100
Number 1 emerges 119 times, error rate is 0.19
Number 2 emerges 90 times, error rate is 0.1
Number 3 emerges 106 times, error rate is 0.06
Number 4 emerges 109 times, error rate is 0.09
Number 5 emerges 83 times, error rate is 0.17
Number 6 emerges 84 times, error rate is 0.16
Number 7 emerges 110 times, error rate is 0.1
Number 8 emerges 86 times, error rate is 0.14
Number 9 emerges 98 times, error rate is 0.02
Number 10 emerges 115 times, error rate is 0.15
Do you want another simulation ? y
Please enter the simulation times : 1000
Number 1 emerges 993 times, error rate is 0.007
Number 2 emerges 993 times, error rate is 0.007
Number 3 emerges 982 times, error rate is 0.018
Number 4 emerges 1017 times, error rate is 0.017
Number 5 emerges 981 times, error rate is 0.019
Number 6 emerges 1026 times, error rate is 0.026
Number 7 emerges 1036 times, error rate is 0.036
Number 8 emerges 1018 times, error rate is 0.018
Number 9 emerges 939 times, error rate is 0.061
Number 10 emerges 1015 times, error rate is 0.015
Do you want another simulation ? y
Please enter the simulation times : 10000
Number 1 emerges 10032 times, error rate is 0.0032
Number 2 emerges 9969 times, error rate is 0.0031
Number 3 emerges 10005 times, error rate is 0.0005
Number 4 emerges 10026 times, error rate is 0.0026
Number 5 emerges 9968 times, error rate is 0.0032
Number 6 emerges 10030 times, error rate is 0.003
Number 7 emerges 9972 times, error rate is 0.0028
Number 8 emerges 10027 times, error rate is 0.0027
Number 9 emerges 10103 times, error rate is 0.0103
Number 10 emerges 9868 times, error rate is 0.0132
Do you want another simulation ? y
Please enter the simulation times : 100000
Number 1 emerges 100361 times, error rate is 0.00361
Number 2 emerges 100583 times, error rate is 0.00583
Number 3 emerges 100292 times, error rate is 0.00292
Number 4 emerges 99486 times, error rate is 0.00514
Number 5 emerges 99959 times, error rate is 0.00041
Number 6 emerges 100016 times, error rate is 0.00016
Number 7 emerges 99525 times, error rate is 0.00475
Number 8 emerges 100013 times, error rate is 0.00013
Number 9 emerges 99891 times, error rate is 0.00109
Number 10 emerges 99874 times, error rate is 0.00126
Do you want another simulation ? y
Please enter the simulation times : 1000000
Number 1 emerges 1001575 times, error rate is 0.001575
Number 2 emerges 999646 times, error rate is 0.000354
Number 3 emerges 1000625 times, error rate is 0.000625
Number 4 emerges 998834 times, error rate is 0.001166
Number 5 emerges 999177 times, error rate is 0.000823
Number 6 emerges 1000790 times, error rate is 0.00079
Number 7 emerges 999248 times, error rate is 0.000752
Number 8 emerges 1001082 times, error rate is 0.001082
Number 9 emerges 999396 times, error rate is 0.000604
Number 10 emerges 999627 times, error rate is 0.000373
Do you want another simulation ? y
Please enter the simulation times : 10000000
Number 1 emerges 9998617 times, error rate is 0.0001383
Number 2 emerges 10003641 times, error rate is 0.0003641
Number 3 emerges 10003598 times, error rate is 0.0003598
Number 4 emerges 9997528 times, error rate is 0.0002472
Number 5 emerges 9999144 times, error rate is 8.56e-005
Number 6 emerges 10000957 times, error rate is 9.57e-005
Number 7 emerges 9999983 times, error rate is 1.7e-006
Number 8 emerges 10002350 times, error rate is 0.000235
Number 9 emerges 9998682 times, error rate is 0.0001318
Number 10 emerges 9995500 times, error rate is 0.00045
Do you want another simulation ? n
板凳
bruceteen [专家分:42660] 发布于 2011-04-15 08:13:00
“每组十个随机数,……则每个数字出现次数应该相等”
------ 如果每十个数种每个数字出现次数都相等,那就必然不是随机数啦!!!
从数学上分析,假如前9个分别是1,2,3……9,按你的说法接下来是10的概率应该是100%,但按随机概率的话,接下来为10的概率应该是1/10。互相矛盾。
从实际生活现象分析,抛硬币,正面朝上,反面朝上,是个随机数。难道你每抛十次,则一定会出现5次正面,5次反面吗?
3 楼
tuanlianshi [专家分:0] 发布于 2011-04-15 17:23:00
每个数字出现的次数当然不可能都绝对相等了. 我的意思是产生1到10之间均匀分布的随机数,当样本数足够多时(例如产生一万个),每个数字出现次数应该非常接近,与预期出现次数差值很小.现在的结果误差不是很小,即分布还不够均匀,想要个误差更小的随机数产生器.
4 楼
cgl_lgs [专家分:21040] 发布于 2011-04-15 18:03:00
楼主需要的是U*X系统中的随机数生成服务,rand是给不了你那么高质量的随机数的。
5 楼
hedge31 [专家分:20] 发布于 2011-04-17 10:56:00
RAND()的分布主要是说,在0~MAX中分布0~MAX/2与 MAX/2~MAX是均匀分布的。。如果你把取10的模。就不一定是你想的那样均匀分布。
6 楼
cgl_lgs [专家分:21040] 发布于 2011-04-17 11:38:00
[quote]RAND()的分布主要是说,在0~MAX中分布0~MAX/2与 MAX/2~MAX是均匀分布的。。如果你把取10的模。就不一定是你想的那样均匀分布。[/quote]
也应该是均匀的喔,只不过rand确实无法给出楼主想要的那种质量。
我来回复