跳到正文

科技·工程

借助梅森旋转原理设计 mt44497

Modulator约 14 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 14 分钟

版本

  1. v2当前
  2. v1

设计想法

众所周知,C++11 标准加入了基于梅森旋转的伪随机数生成器 mt19937,而它名字里的 1993719937 则是梅森素数 219937−12^{19937}-1 的指数部分,下面是截至目前已经发现的所有梅森素数。

序号指数 pp梅森素数 Mp=2p−1M_p = 2^p - 1位数发现年份发现者/验证者
112222−1=32^2 - 1 = 311古代已知
223323−1=72^3 - 1 = 711古代已知
335525−1=312^5 - 1 = 3122古代已知
447727−1=1272^7 - 1 = 12733古代已知
551313213−1=81912^{13} - 1 = 8191441456匿名
661717217−1=1310712^{17} - 1 = 131071661588Cataldi
771919219−1=5242872^{19} - 1 = 524287661588Cataldi
883131231−1=21474836472^{31} - 1 = 214748364710101772Euler
996161261−12^{61} - 119191883Pervushin
10108989289−12^{89} - 127271911Powers
11111071072107−12^{107} - 133331914Powers
12121271272127−12^{127} - 139391876Lucas
13135215212521−12^{521} - 11571571952Robinson
14146076072607−12^{607} - 11831831952Robinson
15151279127921279−12^{1279} - 13863861952Robinson
16162203220322203−12^{2203} - 16646641952Robinson
17172281228122281−12^{2281} - 16876871952Robinson
18183217321723217−12^{3217} - 19699691957Riesel
19194253425324253−12^{4253} - 1128112811961Hurwitz
20204423442324423−12^{4423} - 1133213321961Hurwitz
21219689968929689−12^{9689} - 1291729171963Gillies
22229941994129941−12^{9941} - 1299329931963Gillies
23231121311213211213−12^{11213} - 1337633761963Gillies
24241993719937219937−12^{19937} - 1600260021971Tuckerman
25252170121701221701−12^{21701} - 1653365331978Noll、Nickel
26262320923209223209−12^{23209} - 1698769871979Noll
27274449744497244497−12^{44497} - 113395133951979Nelson、Slowinski
28288624386243286243−12^{86243} - 125962259621982Slowinski
29291105031105032110503−12^{110503} - 133265332651988Colquitt、Welsh
30301320491320492132049−12^{132049} - 139751397511983Slowinski
31312160912160912216091−12^{216091} - 165050650501985Slowinski
32327568397568392756839−12^{756839} - 12278322278321992Slowinski、Gage
33338594338594332859433−12^{859433} - 12587162587161994Slowinski、Gage
34341257787125778721257787−12^{1257787} - 13786323786321996Slowinski、Gage
35351398269139826921398269−12^{1398269} - 14209214209211996Armengaud
36362976221297622122976221−12^{2976221} - 18959328959321997Spence
37373021377302137723021377−12^{3021377} - 19095269095261998Clarkson
38386972593697259326972593−12^{6972593} - 1209896020989601999Hajratwala
39391346691713466917213466917−12^{13466917} - 1405394640539462001Cameron
40402099601120996011220996011−12^{20996011} - 1632043063204302003Shafer
41412403658324036583224036583−12^{24036583} - 1723573372357332004Findley
42422596495125964951225964951−12^{25964951} - 1781623078162302005Nowak
43433040245730402457230402457−12^{30402457} - 1915205291520522005Cooper、Boone
44443258265732582657232582657−12^{32582657} - 1980835898083582006Cooper、Boone
45453715666737156667237156667−12^{37156667} - 111185272111852722008Elvenich
46464264380142643801242643801−12^{42643801} - 112837064128370642009Strindmo
47474311260943112609243112609−12^{43112609} - 112978189129781892008Smith
48485788516157885161257885161−12^{57885161} - 117425170174251702013Cooper
49497420728174207281274207281−12^{74207281} - 122338618223386182016Cooper
50507723291777232917277232917−12^{77232917} - 123249425232494252017Pace
51518258993382589933282589933−12^{82589933} - 124862048248620482018Patrick Laroche
52521362798411362798412136279841−12^{136279841} - 141024320410243202024Luke Durant

可以看到 219937−12^{19937}-1 的后面还有很多梅森素数,而其中有一个 244497−12^{44497}-1 的指数 4449744497 不仅没有后面那么大,而且还好记。于是很容易想到设计一个原理类似的 mt44497。

思路推导

我们先看 mt19937 的原理(这样方便给出一些常数的值)。

1 内部状态

mt19937 内部有一个状态数组 state[624] 共 624624 个整形,这个数也约等于 1993732\dfrac{19937}{32}。

2 递推方式

线性递推

有以下公式:

xk+n=xk+m⊕((upper⁡(xk)∥lower⁡(xk+1))⋅A)x_{k+n}=x_{k+m} \oplus ((\operatorname{upper}(x_k) \parallel \operatorname{lower}(x_{k+1}))\cdot A)

其中,upper⁡(a)\operatorname{upper}(a) 指 aa 的第 3131 位,而 lower⁡(a)\operatorname{lower}(a) 指 aa 的第 00 至第 3030 位,符号 ∥\parallel 表示位拼接,⊕\oplus 是异或。 在 mt19937 中,n=624n=624,m=397m=397。 AA 是一个矩阵,但是可以用右移与异或简化为:

(x⋅A)={⌊x2⌋LSB⁡(x)=0⌊x2⌋⊕9908B0DF(16)LSB⁡(x)=1(x \cdot A)=\begin{cases} \lfloor \dfrac{x}{2} \rfloor & \operatorname{LSB}(x)=0\\ \lfloor \dfrac{x}{2} \rfloor\oplus 9908B0DF_{(16)} & \operatorname{LSB}(x)=1 \end{cases}

其中 LSB⁡(x)\operatorname{LSB}(x) 表示 xx 的第 00 位。

输出变换

对生成的 xk+nx_{k+n} 进行非线性变换提高质量:

y=xk+n⊕⌊xk+n2u⌋y=y⊕((y×2s)and⁡b)y=y⊕((y×2t)and⁡c)y=y⊕⌊y2l⌋y=x_{k+n} \oplus \lfloor \dfrac{x_{k+n}}{2^u} \rfloor\\ y=y \oplus ((y \times 2^s) \operatorname{and} b)\\ y=y \oplus ((y \times 2^t) \operatorname{and} c)\\ y=y \oplus \lfloor \dfrac{y}{2^l} \rfloor

在 mt19937 中,u=11,s=7,t=15,l=18,b=9D2C5680(16),c=EFC60000(16)u=11,s=7,t=15,l=18,b=9D2C5680_{(16)},c=EFC60000_{(16)}。

yy 即是返回的结果,具体地,mt19937 在每生成 624624 个随机数后再统一生成 624624 个整数在 state 中。

3 让我们类推一下

我们对于 mt44497 发现参数基本上可以照搬,只有像 n,mn,m 这样的数需要修改。 于是我们就得到了 mt44497 的基本代码实现。

代码实现

这里使用无符号 3232 位整数。

C++
class mt44497{
	public:
	    typedef unsigned int uint32;
	    enum{STATE_SIZE=1392,DEFAULT_SEED=5489U,MAX_VALUE=0xFFFFFFFFU};
	    explicit mt44497(uint32 Seed=DEFAULT_SEED){
	        seed(Seed);
	    }
	    void seed(uint32 seed=DEFAULT_SEED){
	        state[0]=seed;
	        for(pos=1;pos<STATE_SIZE;++pos){
	            state[pos]=1812433253U*(state[pos-1]^(state[pos-1]>>30))+pos;
	        }
	    }
	    uint32 operator()(){
	        if(pos>=STATE_SIZE){twistState();}
	        uint32 y=state[pos++];
	        y^=(y>>15)|(y<<17);
	        y^=(y>>11);
	        y^=(y<<7)&0x9D2C5680U;
	        y=(y&0xFFFFFF00)|((y^(y>>8))&0xFFFFFF);
	        y^=(y<<15)&0xEFC60000U;
	        y^=(y<<18);
	        return y;
	    }
	    uint32 min()const{return 0;}
	    uint32 max()const{return MAX_VALUE;}
	private:
	    void twistState(){
	        const uint32 MATRIX_A=0x9908B0DFU;
	        const uint32 UPPER_MASK=0x80000000U;
	        const uint32 LOWER_MASK=0x7FFFFFFFU;
	        for(int k=0;k<STATE_SIZE;++k){
	            uint32 x=(state[k]&UPPER_MASK)|(state[(k+1)%STATE_SIZE]&LOWER_MASK);
	            state[k]=state[(k+397)%STATE_SIZE]^(x>>1);
	            if(x&0x1U){
	                state[k]^=MATRIX_A;
	            }
	        }
	        pos=0;
	    }
	    uint32 state[STATE_SIZE];
	    int pos;
};
原文