Repository navigation
Expand file tree
/
Copy path_random.cs
More file actions
151 lines (131 loc) · 5.09 KB
/
Copy path_random.cs
File metadata and controls
151 lines (131 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* [email protected]. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System;
using System.Numerics;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
using Microsoft.Scripting.Utils;
#if FEATURE_SERIALIZATION
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
#endif
[assembly: PythonModule("_random", typeof(IronPython.Modules.PythonRandom))]
namespace IronPython.Modules {
public static class PythonRandom {
public const string __doc__ = "implements a random number generator";
[PythonType]
public class Random {
private System.Random _rnd;
public Random() {
seed();
}
public Random(object seed) {
this.seed(seed);
}
#region Public API surface
public object getrandbits(int bits) {
if (bits <= 0) {
throw PythonOps.ValueError("number of bits must be greater than zero");
}
lock (this) {
return _rnd.GetRandBits(bits);
}
}
public object getstate() {
#if FEATURE_SERIALIZATION
MemoryStream stream = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
lock (this) {
formatter.Serialize(stream, _rnd);
}
return PythonTuple.MakeTuple(stream.GetBuffer().Select(x => (int)x).Cast<object>().ToArray());
#else
return _rnd;
#endif
}
public object random() {
lock (this) {
byte[] randA = new byte[sizeof(uint)];
byte[] randB = new byte[sizeof(uint)];
_rnd.NextBytes(randA);
_rnd.NextBytes(randB);
// this is pulled from _randommodule.c from CPython
uint a = BitConverter.ToUInt32(randA, 0) >> 5;
uint b = BitConverter.ToUInt32(randB, 0) >> 6;
return (a*67108864.0+b)*(1.0/9007199254740992.0);
}
}
public void seed() {
seed(DateTime.Now);
}
public void seed(object s) {
if (s == null) {
seed();
return;
}
int newSeed;
if (s is int) {
newSeed = (int)s;
} else {
if (!PythonContext.IsHashable(s)) {
throw PythonOps.TypeError("unhashable type: '{0}'", PythonOps.GetPythonTypeName(s));
}
newSeed = s.GetHashCode();
}
lock (this) {
_rnd = new System.Random(newSeed);
}
}
public void setstate(object state) {
#if FEATURE_SERIALIZATION
PythonTuple s = state as PythonTuple;
if(s == null) {
throw PythonOps.TypeError("state vector must be a tuple");
}
try {
object[] arr = s.ToArray();
byte[] b = new byte[arr.Length];
for (int i = 0; i < arr.Length; i++) {
if (arr[i] is int) {
b[i] = (byte)(int)(arr[i]);
} else {
throw PythonOps.TypeError("state vector of unexpected type: {0}", PythonOps.GetPythonTypeName(arr[i]));
}
}
MemoryStream stream = new MemoryStream(b);
IFormatter formatter = new BinaryFormatter();
lock (this) {
_rnd = (System.Random)formatter.Deserialize(stream);
}
} catch (SerializationException ex) {
throw PythonOps.SystemError("state vector invalid: {0}", ex.Message);
}
#else
System.Random random = state as System.Random;
lock (this) {
if (random != null) {
_rnd = random;
} else {
throw IronPython.Runtime.Operations.PythonOps.TypeError("setstate: argument must be value returned from getstate()");
}
}
#endif
}
#endregion
}
}
}