随机数生成代码演示

// ConsoleApplication5.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include<random>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    default_random_engine g; //随机数引擎,他有一个默认的固定值
    
    uniform_int_distribution<int> dr(1,9);//区间的范围为10-20,且为无符号的int,其区间
    for (int i = 0; i < 20; i++)
    {
        cout<<dr(g)<<",";
    }
    cout << endl;
    

    /*
    uniform_real_distribution<double> dr(10,20);//区间的范围为半开的10-20
    for (int i = 0; i < 9; i++)
    {
        cout << dr(g)<<",";
    }
    cout << endl;
    */
    vector<int> p{1,2,3,4,5,6,7,8,9};

    shuffle(p.begin(),p.end(),g);//用随机数引擎g将p.begin()到p.end()的数打乱.

    for(auto i = p.begin(); i != p.end(); i++)
    {
        cout << *i << ",";
    }
    cout << endl;



    return 0;
}
原文地址:https://www.cnblogs.com/SunShine-gzw/p/13534694.html