0 Members and 1 Guest are viewing this topic.
// Mersenne Twister Random Number Generatorconst // don't touch MT_N = 624; MT_M = 397; MATRIX_A = $9908b0df; UPPER_MASK = $80000000; LOWER_MASK = $7fffffff; TEMPERING_MASK_B = $9d2c5680; TEMPERING_MASK_C = $efc60000;var mt: array[0..MT_N - 1] of Int64; mti: LongInt;procedure MTInit(seed: LongWord);begin mt[0] := seed and $ffffffff; mti := 1; while(mti<MT_N)do begin mt[mti] := ((69069 * mt[mti-1]) and $ffffffff); mti := mti + 1; end;end;function GenRandIntMT: LongWord;var mag01: array[0..1] of LongWord; y: LongWord; kk: LongInt;begin mag01[0]:=$0; mag01[1]:=MATRIX_A; if (mti >= MT_N) then begin if (mti = (MT_N+1))then MTInit(4357); kk := 0; while(kk<(MT_N-MT_M))do begin y := (mt[kk]and UPPER_MASK)or(mt[kk+1]and LOWER_MASK); mt[kk] := mt[kk+MT_M] xor (y shr 1) xor mag01[y and $1]; kk := kk + 1; end; while(kk<(MT_N-1))do begin y := (mt[kk]and UPPER_MASK)or(mt[kk+1]and LOWER_MASK); mt[kk] := mt[kk+(MT_M-MT_N)] xor (y shr 1) xor mag01[y and $1]; kk := kk + 1; end; y := (mt[MT_N-1]and UPPER_MASK)or(mt[0]and LOWER_MASK); mt[MT_N-1] := mt[MT_M-1] xor (y shr 1) xor mag01[y and $1]; mti := 0; end; y := mt[mti]; mti := mti + 1; y := y xor (y shr 11); y := y xor (y shl 7) and TEMPERING_MASK_B; y := y xor (y shl 15) and TEMPERING_MASK_C; y := y xor (y shr 18); Result := y;end;function RandInt(min, max: integer): Integer;begin Result := max - min + 1; max := GenRandIntMT; if max < 0 then max := -max; Result := min + max mod Result;end;function RandFlt(min, max: Single): Single;begin Result := min + (max - min) * GenRandIntMT * 2.3283064370807974e-10;end;// generator must be initialized with a nonzero value on startprocedure ActivateServer();var i: integer;begin repeat i := Random(-$FFFFFF, $FFFFFF); until (i <> 0); MTInit(i);end;
MTInit(2); A := GenRandIntMT();MTInit(2); B := GenRandIntMT();if (A = B) then WriteLn('EqUaL! equal');
procedure MTInit(seed: LongWord); - if we put the same seed in again, we would get the same random numbers?
I suggest function RandFlt(min, max: Single): Double; or even possible extended (or all three in different functions but thats probably unnecessary)