[C++基础]随机数,随机种子数

本文详细探讨了C++中如何生成随机数,包括随机数生成器的使用,随机种子的概念及其重要性,以及如何通过设置种子来确保随机数序列的可重复性和不可预测性。通过对随机数函数和种子管理的实例解析,帮助读者掌握C++中随机数的运用技巧。
摘要由CSDN通过智能技术生成


#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<<"\n指定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<<"\n指定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<<"\n指定seed当前系统时间, ";
    for(int i=0; i<10;i++
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值