-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathPyRandom.java
More file actions
139 lines (124 loc) · 4.46 KB
/
PyRandom.java
File metadata and controls
139 lines (124 loc) · 4.46 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
package org.python.modules.random;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.util.Random;
import org.python.core.Py;
import org.python.core.PyFloat;
import org.python.core.PyInteger;
import org.python.core.PyLong;
import org.python.core.PyObject;
import org.python.core.PyTuple;
import org.python.core.PyType;
import org.python.core.Untraversable;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
@Untraversable
@ExposedType(name = "_random.Random")
public class PyRandom extends PyObject {
public static final PyType TYPE = PyType.fromClass(PyRandom.class);
public PyRandom() {
this(TYPE);
}
public PyRandom(PyType subType) {
super(subType);
}
// added functions
protected Random javaRandom = new Random();
/**
* Sets the seed of the internal number generated to seed. If seed is a PyInteger or PyLong, it
* uses the value, else it uses the hash function of PyObject
*/
@ExposedMethod(defaults = "null")
final void Random_seed(PyObject seed) {
long n;
if (seed == null) {
seed = new PyLong(System.currentTimeMillis());
}
if (seed instanceof PyLong) {
PyLong max = new PyLong(Long.MAX_VALUE);
n = seed.__mod__(max).asLong();
} else if (seed instanceof PyInteger) {
n = seed.asLong();
} else {
n = seed.hashCode();
}
this.javaRandom.setSeed(n);
}
@ExposedNew
@ExposedMethod
final void Random___init__(PyObject[] args, String[] keywords) {}
@ExposedMethod
final void Random_jumpahead(PyObject arg0) {
if (!(arg0 instanceof PyInteger || arg0 instanceof PyLong)) {
throw Py.TypeError(String.format("jumpahead requires an integer, not '%s'",
arg0.getType().fastGetName()));
}
for (long i = arg0.asLong(); i > 0; i--) {
this.javaRandom.nextInt();
}
}
@ExposedMethod
final void Random_setstate(PyObject arg0) {
if (!(arg0 instanceof PyTuple)) {
throw Py.TypeError("state vector must be a tuple");
}
try {
Object arr[]=((PyTuple)arg0).toArray();
byte b[]=new byte[arr.length];
for(int i=0;i<arr.length;i++) {
if (arr[i] instanceof Integer) {
b[i]=((Integer)arr[i]).byteValue();
} else {
throw Py.TypeError("state vector of unexpected type: "+
arr[i].getClass());
}
}
ByteArrayInputStream bin=new ByteArrayInputStream(b);
ObjectInputStream oin=new ObjectInputStream(bin);
this.javaRandom=(java.util.Random)oin.readObject();
} catch (IOException e) {
throw Py.SystemError("state vector invalid: "+e.getMessage());
} catch (ClassNotFoundException e) {
throw Py.SystemError("state vector invalid: "+e.getMessage());
}
}
@ExposedMethod
final PyObject Random_getstate() {
try {
ByteArrayOutputStream bout=new ByteArrayOutputStream();
ObjectOutputStream oout=new ObjectOutputStream(bout);
oout.writeObject(this.javaRandom);
byte b[]=bout.toByteArray();
PyInteger retarr[]=new PyInteger[b.length];
for (int i=0;i<b.length;i++) {
retarr[i]=new PyInteger(b[i]);
}
PyTuple ret=new PyTuple(retarr);
return ret;
} catch (IOException e) {
throw Py.SystemError("creation of state vector failed: "+
e.getMessage());
}
}
/**
* Generate a random number on [0,1) with 53-bit resolution. Implementation lifted from
* _randommodule.c:random_random(), but we use >>> instead of >> to avoid sign
* problems.
*/
@ExposedMethod
final PyObject Random_random() {
long a=this.javaRandom.nextInt()>>>5;
long b=this.javaRandom.nextInt()>>>6;
double ret=(a*67108864.0+b)*(1.0/9007199254740992.0);
return new PyFloat(ret);
}
@ExposedMethod
final PyLong Random_getrandbits(int k) {
return new PyLong(new BigInteger(k, javaRandom));
}
}