美文网首页
C++随机数种子 mt19937

C++随机数种子 mt19937

作者: 猩猩隊長 | 来源:发表于2018-11-08 21:01 被阅读0次

可快速产生高质量的伪随机数

示例:

#include <random>
#include <iostream>

int main()
{
    std::random_device rd;  //获取随机数种子
    std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
    std::uniform_int_distribution<> dis(0, 9);

    for (int n = 0; n<20; ++n)
        std::cout << dis(gen) << ' ';
    std::cout << '\n';
    system("pause");
    return 0;
}

//可能的结果:7 2 2 1 4 1 4 0 4 7 2 1 0 9 1 9 2 3 5 1

此随机数种子没有时间种子那种问题,非常实用

详细说明:

https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution

C++算法实现:

#include <stdint.h>

// Define MT19937 constants (32-bit RNG)
enum
{
    // Assumes W = 32 (omitting this)
    N = 624,
    M = 397,
    R = 31,
    A = 0x9908B0DF,

    F = 1812433253,

    U = 11,
    // Assumes D = 0xFFFFFFFF (omitting this)

    S = 7,
    B = 0x9D2C5680,

    T = 15,
    C = 0xEFC60000,

    L = 18,

    MASK_LOWER = (1ull << R) - 1,
    MASK_UPPER = (1ull << R)
};

static uint32_t  mt[N];
static uint16_t  index;

// Re-init with a given seed
void Initialize(const uint32_t  seed)
{
    uint32_t  i;

    mt[0] = seed;

    for ( i = 1; i < N; i++ )
    {
        mt[i] = (F * (mt[i - 1] ^ (mt[i - 1] >> 30)) + i);
    }

    index = N;
}

static void Twist()
{
    uint32_t  i, x, xA;

    for ( i = 0; i < N; i++ )
    {
        x = (mt[i] & MASK_UPPER) + (mt[(i + 1) % N] & MASK_LOWER);

        xA = x >> 1;

        if ( x & 0x1 )
            xA ^= A;

        mt[i] = mt[(i + M) % N] ^ xA;
    }

    index = 0;
}

// Obtain a 32-bit random number
uint32_t ExtractU32()
{
    uint32_t  y;
    int       i = index;

    if ( index >= N )
    {
        Twist();
        i = index;
    }

    y = mt[i];
    index = i + 1;

    y ^= (mt[i] >> U);
    y ^= (y << S) & B;
    y ^= (y << T) & C;
    y ^= (y >> L);

    return y;
}

另外一种洗牌算法:

#include <random>
#include <algorithm>
#include <iterator>
#include <iostream>

int main()
{
    std::vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    std::random_device rd;
    std::mt19937 g(rd());

    std::shuffle(v.begin(), v.end(), g);

    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << "\n";

    system("pause");
    return 0;
}

相关文章

  • C++随机数种子 mt19937

    可快速产生高质量的伪随机数 示例: 此随机数种子没有时间种子那种问题,非常实用 详细说明: https://en....

  • C++ STL mt19937 使用说明

    说明 std::mt19937是伪随机数产生器,用于产生高性能的随机数。 C++11引入。返回值为unsigned...

  • random库

    seed()给随机数一个种子值,,默认随机种子是系统时钟。 随机种子相同,随机数相同。伪随机数。 random()...

  • Python笔记:Numpy常用方法-2

    Numpy随机函数 # 指定随机数种子,相同的随机数种子,生成相同的随机数 np.random.seed(10) ...

  • random随机函数

    import random seed([1]) #随机数种子要每次产生随机数相同就要设置种子,相同种子数的Ran...

  • c++随机数

    c++产生若干随机数 产生某一区间的随机数

  • set.seed()函数

    set.seed():该命令的作用是设定生成随机数的种子,种子是为了让结果具有重复性。如果不设定种子,生成的随机数...

  • 喵神swifter学习笔记

    1、随机数 不需要随机数种子 arc4random()%N + begin:产生begin~begin+N的随机数...

  • 无处不在的随机数

    目录: 什么是随机数 随机数分类 伪随机数生成器 真随机数生成器 各种语言中的随机数 使用系统时间作为种子是否安全...

  • 数据分析学习笔记(五)-- numpy:随机数

    常用的random函数 uniform :产生low到high之间到随机数 seed :设置随机数种子 注:see...

网友评论

      本文标题:C++随机数种子 mt19937

      本文链接:https://www.haomeiwen.com/subject/bgdgxqtx.html