C/C++ 生成[X,Y]内的随机数

来源:互联网 发布:世辉律师事务所 知乎 编辑:程序博客网 时间:2024/06/09 22:28

   在C语言中,rand()函数可以用来产生随机数,但是这不是真真意义上的随机数,是一个伪随机数。是根据一个数,称为种子,为基准以某个递推公式推算出来的一系数,当这系列数很大的时候,就符合正态公布,从而相当于产生了随机数,但这不是真正的随机数

    rand()会返回一随机数值,范围在0至RAND_MAX 间。返回0至RAND_MAX之间的随机数值,RAND_MAX定义在stdlib.h

在调用此函数rand()产生随机数前,必须先利用srand()设好随机数种子,如果未设随机数种子,rand()在调用时会自动设随机数种子为1,这样每次调用生成的随机数都是一致的。

如何每次产生不一致的随机数:

   srand((int)time(0));//用系统时间来做种子

rand()和srand()函数。这二个函数的工作过程如下:

   1) 首先给srand()提供一个种子,它是一个unsigned int类型,其取值范围从0~65535;

   2) 然后调用rand(),它会根据提供给srand()的种子值返回一个随机数(在0到32767之间)

   3) 根据需要多次调用rand(),从而不间断地得到新的随机数;

例子:

#include<stdio.h>#include<stdlib.h>#include<time.h>void main(){//         srand((int)time(0));    int x;    int j;    for(x=0;x<10;x++)        printf("%d\t",rand_X(100));//输出100内的随机数}int rand_X(int x){    return rand()%x;}
第1次运行结果:

41      67      34      0       69      24      78      58      62      64
第2次运行结果:

41      67      34      0       69      24      78      58      62      64
......

第n次运行结果:

41      67      34      0       69      24      78      58      62      64
每次运行产生不一样的随机数:使用系统时间作为种子

例子:

#include<stdio.h>#include<stdlib.h>#include<time.h>void main(){    srand((int)time(0));//使用系统时间作为随机种子    int x;    int j;    for(x=0;x<10;x++)        printf("%d\t",rand_X(100));//输出100内的随机数}int rand_X(int x){    return rand()%x;}
第1次运行:

84      9       54      43      84      24      48      39      71      54
第2次运行:

88      10      51      37      70      7       43      2       54      24
......

第n次运行:

32      87      43      47      59      43      18      74      67      53

如何产生设定范围内的随机数 

要让随机数限定在一个范围,可以采用模除加法的方式。要产生随机数r, 其范围为 X<=r<=Y,可以使用如下公式:rand()%(Y-X+1)+X其原理为,对于任意数,0<=rand()%(Y-X+1)<=Y-X于是0+X<=rand()%(Y-X+1)+X<=Y-X+X即X<=rand()%(Y-X+1)+X<=Y
代码验证:

/**生成[X,Y]的随机数//rand()%(Y-X+1)+X*/#include<stdio.h>#include<stdlib.h>#include<time.h>void main(){//    int flags[11];//标志位    int i=0;    srand((int)time(0));//用系统时间作为随机种子    int temp=-1;    int Y=10;    int X=0;    //验证确实生成了[0,10]的随机数    while(i<=10)    {        temp=rand()%(Y-X+1)+X;//生成[0,10]的随机数        if(temp>10||temp<0)        {            printf("错误!\n");            break;        }        if(i==temp)        {            printf("temp=%d\n",temp);            i++;        }    }}
结果:

temp=0temp=1temp=2temp=3temp=4temp=5temp=6temp=7temp=8temp=9temp=10Process returned 8 (0x8)   execution time : 0.250 sPress any key to continue.
可以看到并没有输出错误:可见确实生成了[0,10]的随机整数

C++实现:

这里和C语言没什么区别,就是输入输出由printf,scanf,改成cout,cin而已。以及对应都头文件上多加了一个c:#include<ctime>,#includ<cstdlib>

/**C++中获取随机数[x,y]*/#include<iostream>#include<ctime>#include<cstdlib>using namespace std;int main(){    srand((unsigned)time(NULL));//    srand((int)time(0));//这两句结果都是一样的    int Y=10;    int X=0;    int temps[11];    int i=0;    int temp;    while(i<=10)    {        temp=rand()%(Y-X+1)+X;        if(temp>10||temp<0)        {            cout<<"错误!"<<endl;        }        if(temp==i)        {            temps[i]=temp;            i++;        }    }    cout<<"i="<<i<<endl;    for(i=0;i<=10;i++)    {        cout<<temps[i]<<" ";    }    return 0;}
结果:

i=110 1 2 3 4 5 6 7 8 9 10Process returned 0 (0x0)   execution time : 0.664 sPress any key to continue.


原创粉丝点击