![]() |
00001 // MTRandom.cpp: implementation of the MTRandom class. 00002 // 00004 00005 #include "MTRandom.h" 00006 #include <stdio.h> 00007 #include <string.h> 00008 00009 // the following initial values for state were generated by calling seed(5489UL) and saving them with saveState() 00010 unsigned long MTRandom::state[]={5489UL, 1301868182UL, 2938499221UL, 2950281878UL, 1875628136UL, 751856242UL, 944701696UL, 2243192071UL, 694061057UL, 219885934UL, 2066767472UL, 3182869408UL, 485472502UL, 2336857883UL, 1071588843UL, 3418470598UL, 951210697UL, 3693558366UL, 2923482051UL, 1793174584UL, 2982310801UL, 1586906132UL, 1951078751UL, 1808158765UL, 1733897588UL, 431328322UL, 4202539044UL, 530658942UL, 1714810322UL, 3025256284UL, 3342585396UL, 1937033938UL, 2640572511UL, 1654299090UL, 3692403553UL, 4233871309UL, 3497650794UL, 862629010UL, 2943236032UL, 2426458545UL, 1603307207UL, 1133453895UL, 3099196360UL, 2208657629UL, 2747653927UL, 931059398UL, 761573964UL, 3157853227UL, 785880413UL, 730313442UL, 124945756UL, 2937117055UL, 3295982469UL, 1724353043UL, 3021675344UL, 3884886417UL, 4010150098UL, 4056961966UL, 699635835UL, 2681338818UL, 1339167484UL, 720757518UL, 2800161476UL, 2376097373UL, 1532957371UL, 3902664099UL, 1238982754UL, 3725394514UL, 3449176889UL, 3570962471UL, 4287636090UL, 4087307012UL, 3603343627UL, 202242161UL, 2995682783UL, 1620962684UL, 3704723357UL, 371613603UL, 2814834333UL, 2111005706UL, 624778151UL, 2094172212UL, 4284947003UL, 1211977835UL, 991917094UL, 1570449747UL, 2962370480UL, 1259410321UL, 170182696UL, 146300961UL, 2836829791UL, 619452428UL, 2723670296UL, 1881399711UL, 1161269684UL, 1675188680UL, 4132175277UL, 780088327UL, 3409462821UL, 1036518241UL, 1834958505UL, 3048448173UL, 161811569UL, 618488316UL, 44795092UL, 3918322701UL, 1924681712UL, 3239478144UL, 383254043UL, 4042306580UL, 2146983041UL, 3992780527UL, 3518029708UL, 3545545436UL, 3901231469UL, 1896136409UL, 2028528556UL, 2339662006UL, 501326714UL, 2060962201UL, 2502746480UL, 561575027UL, 581893337UL, 3393774360UL, 1778912547UL, 3626131687UL, 2175155826UL, 319853231UL, 986875531UL, 819755096UL, 2915734330UL, 2688355739UL, 3482074849UL, 2736559UL, 2296975761UL, 1029741190UL, 2876812646UL, 690154749UL, 579200347UL, 4027461746UL, 1285330465UL, 2701024045UL, 4117700889UL, 759495121UL, 3332270341UL, 2313004527UL, 00011 2277067795UL, 4131855432UL, 2722057515UL, 1264804546UL, 3848622725UL, 2211267957UL, 4100593547UL, 959123777UL, 2130745407UL, 3194437393UL, 486673947UL, 1377371204UL, 17472727UL, 352317554UL, 3955548058UL, 159652094UL, 1232063192UL, 3835177280UL, 49423123UL, 3083993636UL, 733092UL, 2120519771UL, 2573409834UL, 1112952433UL, 3239502554UL, 761045320UL, 1087580692UL, 2540165110UL, 641058802UL, 1792435497UL, 2261799288UL, 1579184083UL, 627146892UL, 2165744623UL, 2200142389UL, 2167590760UL, 2381418376UL, 1793358889UL, 3081659520UL, 1663384067UL, 2009658756UL, 2689600308UL, 739136266UL, 2304581039UL, 3529067263UL, 591360555UL, 525209271UL, 3131882996UL, 294230224UL, 2076220115UL, 3113580446UL, 1245621585UL, 1386885462UL, 3203270426UL, 123512128UL, 12350217UL, 354956375UL, 4282398238UL, 3356876605UL, 3888857667UL, 157639694UL, 2616064085UL, 1563068963UL, 2762125883UL, 4045394511UL, 4180452559UL, 3294769488UL, 1684529556UL, 1002945951UL, 3181438866UL, 22506664UL, 691783457UL, 2685221343UL, 171579916UL, 3878728600UL, 2475806724UL, 2030324028UL, 3331164912UL, 1708711359UL, 1970023127UL, 2859691344UL, 2588476477UL, 2748146879UL, 136111222UL, 2967685492UL, 909517429UL, 2835297809UL, 3206906216UL, 3186870716UL, 341264097UL, 2542035121UL, 3353277068UL, 548223577UL, 3170936588UL, 1678403446UL, 297435620UL, 2337555430UL, 466603495UL, 1132321815UL, 1208589219UL, 696392160UL, 894244439UL, 2562678859UL, 470224582UL, 3306867480UL, 201364898UL, 2075966438UL, 1767227936UL, 2929737987UL, 3674877796UL, 2654196643UL, 3692734598UL, 3528895099UL, 2796780123UL, 3048728353UL, 842329300UL, 191554730UL, 2922459673UL, 3489020079UL, 3979110629UL, 1022523848UL, 2202932467UL, 3583655201UL, 3565113719UL, 587085778UL, 4176046313UL, 3013713762UL, 950944241UL, 396426791UL, 3784844662UL, 3477431613UL, 3594592395UL, 2782043838UL, 3392093507UL, 3106564952UL, 2829419931UL, 1358665591UL, 2206918825UL, 3170783123UL, 31522386UL, 2988194168UL, 1782249537UL, 1105080928UL, 843500134UL, 1225290080UL, 1521001832UL, 3605886097UL, 00012 2802786495UL, 2728923319UL, 3996284304UL, 903417639UL, 1171249804UL, 1020374987UL, 2824535874UL, 423621996UL, 1988534473UL, 2493544470UL, 1008604435UL, 1756003503UL, 1488867287UL, 1386808992UL, 732088248UL, 1780630732UL, 2482101014UL, 976561178UL, 1543448953UL, 2602866064UL, 2021139923UL, 1952599828UL, 2360242564UL, 2117959962UL, 2753061860UL, 2388623612UL, 4138193781UL, 2962920654UL, 2284970429UL, 766920861UL, 3457264692UL, 2879611383UL, 815055854UL, 2332929068UL, 1254853997UL, 3740375268UL, 3799380844UL, 4091048725UL, 2006331129UL, 1982546212UL, 686850534UL, 1907447564UL, 2682801776UL, 2780821066UL, 998290361UL, 1342433871UL, 4195430425UL, 607905174UL, 3902331779UL, 2454067926UL, 1708133115UL, 1170874362UL, 2008609376UL, 3260320415UL, 2211196135UL, 433538229UL, 2728786374UL, 2189520818UL, 262554063UL, 1182318347UL, 3710237267UL, 1221022450UL, 715966018UL, 2417068910UL, 2591870721UL, 2870691989UL, 3418190842UL, 4238214053UL, 1540704231UL, 1575580968UL, 2095917976UL, 4078310857UL, 2313532447UL, 2110690783UL, 4056346629UL, 4061784526UL, 1123218514UL, 551538993UL, 597148360UL, 4120175196UL, 3581618160UL, 3181170517UL, 422862282UL, 3227524138UL, 1713114790UL, 662317149UL, 1230418732UL, 928171837UL, 1324564878UL, 1928816105UL, 1786535431UL, 2878099422UL, 3290185549UL, 539474248UL, 1657512683UL, 552370646UL, 1671741683UL, 3655312128UL, 1552739510UL, 2605208763UL, 1441755014UL, 181878989UL, 3124053868UL, 1447103986UL, 3183906156UL, 1728556020UL, 3502241336UL, 3055466967UL, 1013272474UL, 818402132UL, 1715099063UL, 2900113506UL, 397254517UL, 4194863039UL, 1009068739UL, 232864647UL, 2540223708UL, 2608288560UL, 2415367765UL, 478404847UL, 3455100648UL, 3182600021UL, 2115988978UL, 434269567UL, 4117179324UL, 3461774077UL, 887256537UL, 3545801025UL, 286388911UL, 3451742129UL, 1981164769UL, 786667016UL, 3310123729UL, 3097811076UL, 2224235657UL, 2959658883UL, 3370969234UL, 2514770915UL, 3345656436UL, 2677010851UL, 2206236470UL, 271648054UL, 2342188545UL, 4292848611UL, 3646533909UL, 00013 3754009956UL, 3803931226UL, 4160647125UL, 1477814055UL, 4043852216UL, 1876372354UL, 3133294443UL, 3871104810UL, 3177020907UL, 2074304428UL, 3479393793UL, 759562891UL, 164128153UL, 1839069216UL, 2114162633UL, 3989947309UL, 3611054956UL, 1333547922UL, 835429831UL, 494987340UL, 171987910UL, 1252001001UL, 370809172UL, 3508925425UL, 2535703112UL, 1276855041UL, 1922855120UL, 835673414UL, 3030664304UL, 613287117UL, 171219893UL, 3423096126UL, 3376881639UL, 2287770315UL, 1658692645UL, 1262815245UL, 3957234326UL, 1168096164UL, 2968737525UL, 2655813712UL, 2132313144UL, 3976047964UL, 326516571UL, 353088456UL, 3679188938UL, 3205649712UL, 2654036126UL, 1249024881UL, 880166166UL, 691800469UL, 2229503665UL, 1673458056UL, 4032208375UL, 1851778863UL, 2563757330UL, 376742205UL, 1794655231UL, 340247333UL, 1505873033UL, 396524441UL, 879666767UL, 3335579166UL, 3260764261UL, 3335999539UL, 506221798UL, 4214658741UL, 975887814UL, 2080536343UL, 3360539560UL, 571586418UL, 138896374UL, 4234352651UL, 2737620262UL, 3928362291UL, 1516365296UL, 38056726UL, 3599462320UL, 3585007266UL, 3850961033UL, 471667319UL, 1536883193UL, 2310166751UL, 1861637689UL, 2530999841UL, 4139843801UL, 2710569485UL, 827578615UL, 2012334720UL, 2907369459UL, 3029312804UL, 2820112398UL, 1965028045UL, 35518606UL, 2478379033UL, 643747771UL, 1924139484UL, 4123405127UL, 3811735531UL, 3429660832UL, 3285177704UL, 1948416081UL, 1311525291UL, 1183517742UL, 1739192232UL, 3979815115UL, 2567840007UL, 4116821529UL, 213304419UL, 4125718577UL, 1473064925UL, 2442436592UL, 1893310111UL, 4195361916UL, 3747569474UL, 828465101UL, 2991227658UL, 750582866UL, 1205170309UL, 1409813056UL, 678418130UL, 1171531016UL, 3821236156UL, 354504587UL, 4202874632UL, 3882511497UL, 1893248677UL, 1903078632UL, 26340130UL, 2069166240UL, 3657122492UL, 3725758099UL, 831344905UL, 811453383UL, 3447711422UL, 2434543565UL, 4166886888UL, 3358210805UL, 4142984013UL, 2988152326UL, 3527824853UL, 982082992UL, 2809155763UL, 190157081UL, 3340214818UL, 2365432395UL, 2548636180UL, 00014 2894533366UL, 3474657421UL, 2372634704UL, 2845748389UL, 43024175UL, 2774226648UL, 1987702864UL, 3186502468UL, 453610222UL, 4204736567UL, 1392892630UL, 2471323686UL, 2470534280UL, 3541393095UL, 4269885866UL, 3909911300UL, 759132955UL, 1482612480UL, 667715263UL, 1795580598UL, 2337923983UL, 3390586366UL, 581426223UL, 1515718634UL, 476374295UL, 705213300UL, 363062054UL, 2084697697UL, 2407503428UL, 2292957699UL, 2426213835UL, 2199989172UL, 1987356470UL, 4026755612UL, 2147252133UL, 270400031UL, 1367820199UL, 2369854699UL, 2844269403UL, 79981964UL 00015 }; 00016 int MTRandom::p = 0; 00017 00019 // Construction/Destruction 00021 00022 MTRandom::MTRandom(){ 00023 } 00024 00025 MTRandom::~MTRandom(){ 00026 } 00027 00028 00029 void MTRandom::gen_state() { // generate new state vector 00030 int i; 00031 for (i = 0; i < (MTRandomN - MTRandomM); ++i) 00032 state[i] = state[i + MTRandomM] ^ twiddle(state[i], state[i + 1]); 00033 for (i = MTRandomN - MTRandomM; i < (MTRandomN - 1); ++i) 00034 state[i] = state[i + MTRandomM - MTRandomN] ^ twiddle(state[i], state[i + 1]); 00035 state[MTRandomN - 1] = state[MTRandomM - 1] ^ twiddle(state[MTRandomN - 1], state[0]); 00036 p = 0; // reset position 00037 } 00038 00039 void MTRandom::seed(unsigned long s){ 00040 state[0] = s & 0xFFFFFFFFUL; // for > 32 bit machines 00041 for (int i = 1; i < MTRandomN; ++i) { 00042 state[i] = 1812433253UL * (state[i - 1] ^ (state[i - 1] >> 30)) + i; 00043 // see Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier 00044 // in the previous versions, MSBs of the seed affect only MSBs of the array state 00045 // 2002/01/09 modified by Makoto Matsumoto 00046 state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines 00047 } 00048 p = MTRandomN; // force gen_state() to be called for next random number 00049 } 00050 00051 void MTRandom::seed(const unsigned long* array, int size) { // init by array 00052 seed(19650218UL); 00053 int i = 1, j = 0, k; 00054 for (k = ((MTRandomN > size) ? MTRandomN : size); k; --k) { 00055 state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1664525UL)) 00056 + array[j] + j; // non linear 00057 state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines 00058 ++j; j %= size; 00059 if ((++i) == MTRandomN) { state[0] = state[MTRandomN - 1]; i = 1; } 00060 } 00061 for (k = MTRandomN - 1; k; --k) { 00062 state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1566083941UL)) - i; 00063 state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines 00064 if ((++i) == MTRandomN) { state[0] = state[MTRandomN - 1]; i = 1; } 00065 } 00066 state[0] = 0x80000000UL; // MSB is 1; assuring non-zero initial array 00067 p = MTRandomN; // force gen_state() to be called for next random number 00068 } 00069 00070 /* 00071 void MTRandom::saveState(){ 00072 FILE* f=fopen("state.txt","w"); 00073 char s[100]; 00074 int l=0; 00075 for (int i = 0; i < MTRandomN; ++i) { 00076 sprintf(s,"%luUL, ",state[i]); 00077 l+=strlen(s); 00078 if(l>2000) { 00079 fprintf(f,"\n"); 00080 l=0; 00081 } 00082 printf("%d\t%lu\n",i,state[i]); 00083 fprintf(f,"%luUL, ",state[i]); 00084 } 00085 fclose(f); 00086 } 00087 00088 void MTRandom::verifyState(unsigned long s){ 00089 if(state[0] != (s & 0xFFFFFFFFUL)) printf("%d error\n",0); 00090 for (int i = 1; i < MTRandomN; ++i) { 00091 if(state[i] != (1812433253UL * (state[i - 1] ^ (state[i - 1] >> 30)) + i) ) printf("%i error\n",i); 00092 } 00093 } 00094 */
![]() |
Thyrix homepage Users' guide
(C) Arxia 2004-2005