使用C++ 11自带的库函数生成随机数

#include <random>
#include <iostream>
using namespace std;

void random_number() {
    default_random_engine g;
    uniform_int_distribution<int> rand(0,9);
    for (int i=0; i<10; i++) cout << rand(g) << " ";
    cout << endl;
    uniform_real_distribution<float> uniform(0,1);
    for (int i=0; i<10; i++) cout << uniform(g) << " ";
    cout << endl;
}

int main() {
    random_number();
    return 0;
}

  

需要C++ 11 支持。

posted @ 2015-09-10 14:56  站在边缘的人  阅读(273)  评论(0编辑  收藏  举报