zoukankan      html  css  js  c++  java
  • [C++基础]随机数,随机种子数


    #include <stdlib.h>
    #include <iostream>
    #include <ctime>
    using namespace std;
    void Test()
    {
        int ran_num = 0;
        cout<<"不指定seed,   ";
        for(int i=0; i<10;i++)
        {
            ran_num = rand()%6;
            cout<<ran_num<<" ";
        }//每次执行都将输出:5,5,4,4,5,4,0,0,4,2
    
        srand(1);
        cout<<"
    指定seed为1,  ";
        for(int i=0; i<10;i++)
        {
            ran_num = rand()%6;
            cout<<ran_num<<" ";
        }//每次执行都将输出:5,5,4,4,5,4,0,0,4,2
    
        srand(6);
        cout<<"
    指定seed为6,  ";
        for(int i=0; i<10;i++)
        {
            ran_num = rand()%6;
            cout<<ran_num<<" ";
        }//每次执行都将输出:5,5,4,4,5,4,0,0,4,2
        srand((unsigned)time(NULL));
        cout<<"
    指定seed当前系统时间, ";
        for(int i=0; i<10;i++)
        {
            ran_num = rand()%6;
            cout<<ran_num<<" ";
        }//每次执行结果都不一样
    }
    /*
    1.随机数也随机种子数之间的关系:随机种子是用来打乱随机数的,没有它,你的随机数并非真正随机
    2.种子与结果的关系是:对于不同的种子,有不同的随机数数列。对于同样的种子。具有同样的随机数数列
    3.一个项目中(可执行文件)。就须要设置一次随机种子
    */
    int main()
    {
        Test();
    	return 0;
    }


  • 相关阅读:
    expect简介和使用例子(转)
    openshift网络
    openstack相关
    SDN的开源方案sonic
    OpenStack Neutron ML2 Deep Dive
    2017双11技术揭秘—阿里数据库计算存储分离与离在线混布
    es的gui工具
    django orm中blank和null的区别
    断关联多表关系阐述
    视图使用
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5353835.html
Copyright © 2011-2022 走看看