File tree Expand file tree Collapse file tree 4 files changed +164
-0
lines changed Expand file tree Collapse file tree 4 files changed +164
-0
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
2
+
3
+ namespace AleaPRNG
4
+ {
5
+ public class Alea
6
+ {
7
+ private string seed ;
8
+ private AleaState state ;
9
+
10
+
11
+ public Alea ( string seed )
12
+ {
13
+ this . seed = seed ;
14
+ state = AleaHandler . InitializeState ( seed ) ;
15
+ }
16
+
17
+
18
+ public AleaState ExportState ( )
19
+ {
20
+ return state ;
21
+ }
22
+
23
+
24
+ public Alea ImportState ( AleaState state )
25
+ {
26
+ this . state = state ;
27
+ return this ;
28
+ }
29
+
30
+
31
+ public double Next ( )
32
+ {
33
+ var data = AleaHandler . AdvanceState ( state ) ;
34
+ state = data . Value ;
35
+ return data . Key ;
36
+ }
37
+
38
+
39
+ public double Random ( )
40
+ {
41
+ return Next ( ) ;
42
+ }
43
+
44
+
45
+ public Alea Clone ( )
46
+ {
47
+ return new Alea ( seed ) . ImportState ( ExportState ( ) ) ;
48
+ }
49
+ }
50
+ }
Original file line number Diff line number Diff line change
1
+ using System . Collections . Generic ;
2
+
3
+ namespace AleaPRNG
4
+ {
5
+ public static class AleaHandler
6
+ {
7
+ public static AleaState InitializeState ( string seed )
8
+ {
9
+ Mash mash = new Mash ( ) ;
10
+
11
+
12
+ var s0 = mash . RunMash ( " " ) ;
13
+ var s1 = mash . RunMash ( " " ) ;
14
+ var s2 = mash . RunMash ( " " ) ;
15
+ var c = 1 ;
16
+
17
+
18
+ s0 -= mash . RunMash ( seed ) ;
19
+
20
+ if ( s0 < 0 )
21
+ {
22
+ s0 += 1 ;
23
+ }
24
+
25
+ s1 -= mash . RunMash ( seed ) ;
26
+
27
+ if ( s1 < 0 )
28
+ {
29
+ s1 += 1 ;
30
+ }
31
+
32
+ s2 -= mash . RunMash ( seed ) ;
33
+
34
+ if ( s2 < 0 )
35
+ {
36
+ s2 += 1 ;
37
+ }
38
+
39
+
40
+ return new AleaState ( )
41
+ {
42
+ c = c ,
43
+ s0 = s0 ,
44
+ s1 = s1 ,
45
+ s2 = s2 ,
46
+ } ;
47
+ }
48
+
49
+
50
+
51
+ public static KeyValuePair < double , AleaState > AdvanceState ( AleaState state )
52
+ {
53
+ var c = state . c ;
54
+ var s0 = state . s0 ;
55
+ var s1 = state . s1 ;
56
+ var s2 = state . s2 ;
57
+
58
+ var t = 2091639 * s0 + c * 2.3283064365386963e-10 ;
59
+
60
+ s0 = s1 ;
61
+ s1 = s2 ;
62
+
63
+ var value = ( s2 = t - ( c = ( int ) t | 0 ) ) ;
64
+
65
+ return new KeyValuePair < double , AleaState > ( value , new AleaState ( )
66
+ {
67
+ c = c ,
68
+ s0 = s0 ,
69
+ s1 = s1 ,
70
+ s2 = s2 ,
71
+ } ) ;
72
+ }
73
+ }
74
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+
3
+ namespace AleaPRNG
4
+ {
5
+ [ Serializable ]
6
+ public class AleaState
7
+ {
8
+ public int c ;
9
+ public double s0 ;
10
+ public double s1 ;
11
+ public double s2 ;
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ namespace AleaPRNG
2
+ {
3
+ public class Mash
4
+ {
5
+ double n = 0xefc8249d ;
6
+
7
+ public double RunMash ( string x )
8
+ {
9
+ var data = x ;
10
+
11
+ for ( var i = 0 ; i < data . Length ; i ++ )
12
+ {
13
+ n += data [ i ] ;
14
+
15
+ var h = 0.02519603282416938 * n ;
16
+ n = ( uint ) h >> 0 ;
17
+ h -= n ;
18
+ h *= n ;
19
+ n = ( uint ) h >> 0 ;
20
+ h -= n ;
21
+ n += h * 0x100000000 ;
22
+ }
23
+
24
+ return ( ( uint ) n >> 0 ) * 2.3283064365386963e-10 ;
25
+ }
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments