• linux下设置rand()随机种子(转)


    srand(设置随机数种子)
             相关函数
             rand 
             表头文件
             #include<stdlib.h>

             定义函数
             void srand (unsigned int seed);

             函数说明
             srand()用来设置rand()产生随机数时的随机数种子。参数seed必须是个整数,通常可以利用geypid()或time(0)的返回值来当做seed。如果每次seed都设相同值,rand()所产生的随机数值每次就会一样。

             返回值

             范例
            
             #include<time.h>
             #include<stdlib.h>
             main()
             {
             int i,j;
             srand((int)time(0));
             for(i=0;i<10;i++)
             {
             j=1+(int)(10.0*rand()/(RAND_MAX+1.0));
             printf(" %d ",j);
             }
             }

             执行
             5 8 8 8 10 2 10 8 9 9
             2 9 7 4 10 3 2 10 8 7

    rand(产生随机数)
    相关函数
    srand

    表头文件
    #include<stdlib.h>

    定义函数
    int rand(void)

    函数说明
    rand()会返回一随机数值,范围在0至RAND_MAX 间。在调用此函数产生随机数前,必须先利用srand()设好随机数种子,如果未设随机数种子,rand()在调用时会自动设随机数种子为1。关于随机数种子请参考srand()。

    返回值
    返回0至RAND_MAX之间的随机数值,RAND_MAX定义在stdlib.h,其值为2147483647。

    范例

    #include<stdlib.h>
    main()
    {
    int i,j;
    for(i=0;i<10;i++)
    {
    j=1+(int)(10.0*rand()/(RAND_MAX+1.0));
    printf("%d ",j);
    }
    }

    执行
    9 4 8 8 10 2 4 8 3 6
    9 4 8 8 10 2 4 8 3 6

    1、rand返回0-RAND_MAX之间均匀分布的伪随机整数。 RAND_MAX必须至少为32767。rand()函数不接受参数,默认以1为种子(即起始值)。随机数生成器总是以相同的种子开始,所以形成的伪随机数列也相同,失去了随机意义。(但这样便于程序调试)
    2、C++中另一函数srand(),可以指定不同的数(无符号整数变元)为种子。但是如果种子相同,伪随机数列也相同。一个办法是让用户输入种子,但是仍然不理想。
    3、 比较理想的是用变化的数,比如时间来作为随机数生成器的种子。 time的值每时每刻都不同。所以种子不同,所以,产生的随机数也不同。
    // C++随机函数(VC program)
    #include <stdlib.h>
    #include <iostream>
    #include <time.h>
    using namespace std;
    #define MAX 100
    int main(int argc, char* argv[])
    {
    srand( (unsigned)time( NULL ) );//srand()函数产生一个以当前时间开始的随机种子
    for (int i=0;i<10;i++)
    cout<<rand()%MAX<<endl;//MAX为最大值,其随机域为0~MAX-1
    return 0;
    }

    http://bbs.csdn.net/topics/320237644

  • 相关阅读:
    真香警告!多线程分类表情包爬取,一起斗图叭(*^▽^*)~~~
    小白入门爬虫快速上手(详细步骤)
    利用selenium尝试爬取豆瓣图书
    OpenCV图像人脸检测及视频中的人脸检测(附源码)
    pyhton爬取爱豆(李易峰)微博评论(附源码)
    Python爬取最爱的电影并下载到本地(附源码)
    [转载]关于RNA的种类和组成
    2020年中国基因测序产业竞争格局全局观
    Improving and correcting the contiguity of long-read genome assemblies of three plant species using optical mapping and chromosome conformation capture data
    三代测序
  • 原文地址:https://www.cnblogs.com/zxpgo/p/2757640.html
Copyright © 2020-2023  润新知