当前位置:文档之家› C++生成随机数

C++生成随机数

C++中常用rand()函数生成随机数,但严格意义上来讲生成的只是伪随机数(pseudo-random integral number)。生成随机数时需要我们指定一个种子,如果在程序内循环,那么下一次生成随机数时调用上一次的结果作为种子。但如果分两次执行程序,那么由于种子相同,生成的“随机数”也是相同的。

在工程应用时,我们一般将系统当前时间(Unix时间)作为种子,这样生成的随机数更接近于实际意义上的随机数。给一下例程如下:

#include
#include
#include
using namespace std;

int main()
{
double random(double,double);
srand(unsigned(time(0)));
for(int icnt = 0; icnt != 10; ++icnt)
cout << "No." << icnt+1 << ": " << int(random(0,10))<< endl;
return 0;
}

double random(double start, double end)
{
return start+(end-start)*rand()/(RAND_MAX + 1.0);
}
/* 运行结果
* No.1: 3
* No.2: 9
* No.3: 0
* No.4: 9
* No.5: 5
* No.6: 6
* No.7: 9
* No.8: 2
* No.9: 9
* No.10: 6
*/
利用这种方法能不能得到完全意义上的随机数呢?似乎9有点多哦?却没有1,4,7?!我们来做一个概率实验,生成1000万个随机数,看0-9这10个数出现的频率是不是大致相同的。程序如下:
#include
#include
#include
#include
using namespace std;

int main()
{
double random(double,double);
int a[10] = {0};
const int Gen_max = 10000000;
srand(unsigned(time(0)));

for(int icnt = 0; icnt != Gen_max; ++icnt)
switch(int(random(0,10)))
{
case 0: a[0]++; break;
case 1: a[1]++; break;
case 2: a[2]++; break;
case 3: a[3]++; break;
case 4: a[4]++; break;
case 5: a[5]++; break;
case 6: a[6]++; break;
case 7: a[7]++; break;
case 8: a[8]++; break;
case 9: a[9]++; break;
default: cerr << "Error!" << endl; exit(-1);
}

for(int icnt = 0; icnt != 10; ++icnt)
cout << icnt << ": " << setw(6) << setiosflags(ios::fixed) << setprecision(2) << double(a[icnt])/Gen_max*100 << "%" << endl;

return 0;
}

double random(double start, double end)
{
return start+(end-start)*rand()/(RAND_MAX + 1.0);
}
/* 运行结果
* 0: 10.01%
* 1: 9.99%
* 2: 9.99%
* 3: 9.99%
* 4: 9.98%
* 5: 10.01%
* 6: 10.02%
* 7: 10.01%
* 8: 10.01%
* 9: 9.99%
*/
可知用这种方法得到的随机数是满足统计规律的。


--------------------------------------------
C++中如何产生随机数2007年04月26日 星期四 08:33 P.M.C++中产生随机数种子对于初学者一直都很困惑.大家知道,在C中有专门的srand(N)函数可以轻松实现这一功能,然而在C++中则要复杂一些.下面是笔者学习的一点心得,希望对大家能有所帮助.(这里我们依

然要借助C标准库中的rand()函数)

函数说明:

int rand(); :返回从[0,MAX)之间的随机整数,这里的MAX与你所定义的数据类型而定;需#include


void srand( unsigned seed ); :设置随机数种子,#include

time_t time( time_t *time ); :返回当前时间,#include

应用举例:
1):
srand(time(0)); //根据系统时间设置随机数种子
int i = rand() % N; //取得区间[0,N)的整数

如要产生1~10之间随机数,则代码如下:

#include
using namespace std;

#include
#include

int main()
{
int t;
srand(time(0)); //seed
t = rand() % 10+ 1; // random number 1-10

cout << t << endl;

return 0;
}



2):
srand(time(0)); //根据系统时间设置随机数种子
float x = rand() * x / RAND_MAX; //返回1/x的概率

3):
srand(time(0)); //根据系统时间设置随机数种子
vector v; ////随机访问数组类型,#include
random_shuffle(v.begin(), v.end()); //STL算法random_shuffle把容器类的元素顺序捣乱


以下源码来自crafty19.3,最强的源码开放的chess程序。注释很清楚,无需多言。
问:
1.Knuth的书中是怎么讲的?该书我无缘拜读。
2.static const unsigned long x[55],这里取55个随机数的理由是什么?
3.能否比较全面地讲讲随机数产生的一些算法或理论,或推荐一些参考资料?
[code]
/*
A 32 bit random number generator. An implementation in C of the algorithm given by
Knuth, the art of computer programming, vol. 2, pp. 26-27. We use e=32, so
we have to evaluate y(n) = y(n - 24) + y(n - 55) mod 2^32, which is implicitly
done by unsigned arithmetic.
*/

unsigned int Random32(void) {
/*
random numbers from Mathematica 2.0.
SeedRandom = 1;
Table[Random[Integer, {0, 2^32 - 1}]
*/
static const unsigned long x[55] = {
1410651636UL, 3012776752UL, 3497475623UL, 2892145026UL, 1571949714UL,
3253082284UL, 3489895018UL, 387949491UL, 2597396737UL, 1981903553UL,
3160251843UL, 129444464UL, 1851443344UL, 4156445905UL, 224604922UL,
1455067070UL, 3953493484UL, 1460937157UL, 2528362617UL, 317430674UL,
3229354360UL, 117491133UL, 832845075UL, 1961600170UL, 1321557429UL,
747750121UL, 545747446UL, 810476036UL, 503334515UL, 4088144633UL,
2824216555UL, 3738252341UL, 3493754131UL, 3672533954UL, 29494241UL,

1180928407UL, 4213624418UL, 33062851UL, 3221315737UL, 1145213552UL,
2957984897UL, 4078668503UL, 2262661702UL, 65478801UL, 2527208841UL,
1960622036UL, 315685891UL, 1196037864UL, 804614524UL, 1421733266UL,
2017105031UL, 3882325900UL, 810735053UL, 384606609UL, 2393861397UL };
static int init = 1;
static unsigned long y[55];
static int j, k;
unsigned long ul;

if (init)
{
int i;

init = 0;
for (i = 0; i < 55; i++) y[i] = x[i];
j = 24 - 1;
k = 55 - 1;
}

ul = (y[k] += y[j]);
if (--j < 0) j = 55 - 1;
if (--k < 0) k = 55 - 1;
return((unsigned int)ul);
} [/code]

对于初学者来说,只需熟练掌握1)种用法,更深层次的随着水平的提升自然会有所领悟.


(以上部分来自"迷路的狼")


另:


一、C++中不能使用random()函数 random函数不是ANSI C标准,不能在gcc,vc等编译器下编译通过。 可改用C++下的rand函数来实现。 1、C++标准函数库提供一随机数生成器rand,返回0-RAND_MAX之间均匀分布的伪随机整数。 RAND_MAX必须至少为32767。rand()函数不接受参数,默认以1为种子(即起始值)。 随机数生成器总是以相同的种子开始,所以形成的伪随机数列也相同,失去了随机意义。(但这样便于程序调试)
2、C++中另一函数srand(),可以指定不同的数(无符号整数变元)为种子。但是如果种子相同,伪随机数列也相同。一个办法是让用户输入种子,但是仍然不理想。
3、 比较理想的是用变化的数,比如时间来作为随机数生成器的种子。 time的值每时每刻都不同。所以种子不同,所以,产生的随机数也不同。
// C++随机函数(VC program)
#include
#include
#include
using namespace std;
#define MAX 100
int main(int argc, char* argv[])
{ srand( (unsigned)time( NULL ) );//srand()函数产生一个以当前时间开始的随机种子.应该放在for等循环语句前面 不然要很长时间等待
for (int i=0;i<10;i++)
cout< return 0;
}
二、rand()的用法
rand()不需要参数,它会返回一个从0到最大随机数的任意整数,最大随机数的大小通常是固定的一个大整数。 这样,如果你要产生0~10的10个整数,可以表达为:
int N = rand() % 11;
这样,N的值就是一个0~10的随机数,如果要产生1~10,则是这样:
int N = 1 + rand() % 11;
总结来说,可以表示为:
a + rand() % n
其中的a是起始值,n是整数的范围。 a + rand() % (b-a+1) 就表示 a~b之间的一个随机数若要0~1的小数,则可以先取得0~10的整数,然后均除以10即可得到随机到十分位的10

个随机小数,若要得到随机到百分位的随机小数,则需要先得到0~100的10个整数,然后均除以100,其它情况依
此类推。
通常rand()产生的随机数在每次运行的时候都是与上一次相同的,这是有意这样设计的,是为了便于程序的调试。若要产生每次不同的随机数,可以使用srand( seed )函数进行随机化,随着seed的不同,就能够产生不同的随机数。
如大家所说,还可以包含time.h头文件,然后使用srand(time(0))来使用当前时间使随机数发生器随机化,这样就可以保证每两次运行时可以得到不同的随机数序列(只要两次运行的间隔超过1秒)。



相关主题
文本预览
相关文档 最新文档