Skip to content

Instantly share code, notes, and snippets.

@oupo
Created November 4, 2009 08:27
Show Gist options
  • Save oupo/225896 to your computer and use it in GitHub Desktop.
Save oupo/225896 to your computer and use it in GitHub Desktop.
// original: http://d.hatena.ne.jp/housuu2002/20091104/1257283619
//孵化乱数列の再現
//cf http://sou31.hp.infoseek.co.jp/pokemon/ran/ran_0.htm#6
#include <iostream>
#include <iomanip>
#include <cstring>
unsigned long mersenne(unsigned long FSeed){ //FSeed が初期Seed
unsigned long A,B,C,t[630],t2[630],E,S[630],k[3]; //変数定義
//第0テーブルの決定
t[0]=FSeed;
for(int i=1;i<624;i++){ t[i] = (((t[i-1]>>30) ^ (t[i-1])) * 0x6c078965 + i) & 0xFFFFFFFF; }
memcpy(t2, t, sizeof t);
//性格値の決定
for(int j=0;j<624;j++){
//A,B,Cの値決定
if(j<227){A=j;B=j+1;C=j+397;}
if(226<j && j<623){A=j;B=j+1;C=j-227;}
if(j==623){A=623;B=0;C=396;}
//k[0],k[1]の数値計算
k[0] = (t2[A] & 0x80000000) | (t2[B] & 0x7FFFFFFF);
k[1] = ((unsigned long)(k[0]/2) ^ t2[C]);
//E(第1テーブルSeed)の数値計算
if(k[0] % 2==1){E=(k[1] ^ 0x9908b0df);}
if(k[0] % 2==0){E=k[1];}
t2[j] = E;
//性格値の決定
k[0] = (((unsigned long)(E/0x800)) ^ E);
k[1] = (((k[0] * 0x80) & 0x9D2C5680) ^ k[0]);
k[2] = (((k[1] * 0x8000) & 0xEFC60000) ^ k[1]);
S[A] = (((unsigned long)(k[2]/0x40000)) ^ k[2]);
}
//結果出力
cout << "初期Seed:0x" << hex << setw(8) << setfill('0') <<t[0] << endl;
for(int i=0;i<20;i++){
cout << dec << setw(3) << i << " 0x" << hex << setw(8) << setfill('0') << S[i] << endl;
}
return S[1];//2番目の孵化乱数を値として返す
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment