伪随机数

Tags
algorithm
Created
Mar 8, 2016 1:22 PM

https://www.zhihu.com/question/20423025/answer/38967574

  1. 线性同余
  2. 平方取中
  3. 更多
next = 1;
function rand()                        // 生成伪随机数
{
    next = next * 1103515245 + 12345;
    return (next / 65536) % 32768;
}


function srand(seed)         // 修改种子
{
    next = seed;
}


srand(Date.now()); rand()

https://v8.dev/blog/math-random

MWC1616:

uint32_t state0 = 1;
uint32_t state1 = 2;
uint32_t mwc1616() {
  state0 = 18030 * (state0 & 0xFFFF) + (state0 >> 16);
  state1 = 30903 * (state1 & 0xFFFF) + (state1 >> 16);
  return state0 << 16 + (state1 & 0xFFFF);
}

xorshift128+:

uint64_t state0 = 1;
uint64_t state1 = 2;
uint64_t xorshift128plus() {
  uint64_t s1 = state0;
  uint64_t s0 = state1;
  state0 = s0;
  s1 ^= s1 << 23;
  s1 ^= s1 >> 17;
  s1 ^= s0;
  s1 ^= s0 >> 26;
  state1 = s1;
  return state0 + state1;
}
SuperMade with Super