File tree Expand file tree Collapse file tree 2 files changed +20
-6
lines changed
graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/random Expand file tree Collapse file tree 2 files changed +20
-6
lines changed Original file line number Diff line number Diff line change 6
6
import com .oracle .graal .python .builtins .objects .type .PythonClass ;
7
7
8
8
public class PRandom extends PythonBuiltinObject {
9
- private Random javaRandom = new Random ();
9
+ private static class PythonRandom extends Random {
10
+ private static final long serialVersionUID = 1L ;
11
+
12
+ long getSeed () {
13
+ int nextseed = this .next (48 ); // 48 magic number of bits shifted away in superclass
14
+ this .setSeed (nextseed );
15
+ return nextseed ;
16
+ }
17
+ }
18
+
19
+ private PythonRandom javaRandom = new PythonRandom ();
10
20
11
21
public PRandom (PythonClass cls ) {
12
22
super (cls );
@@ -16,6 +26,10 @@ public void setSeed(long seed) {
16
26
javaRandom .setSeed (seed );
17
27
}
18
28
29
+ public long getSeed () {
30
+ return javaRandom .getSeed ();
31
+ }
32
+
19
33
public long nextLong () {
20
34
return javaRandom .nextLong ();
21
35
}
@@ -28,7 +42,7 @@ public Random getJavaRandom() {
28
42
return javaRandom ;
29
43
}
30
44
31
- public void setJavaRandom ( Random random ) {
32
- javaRandom = random ;
45
+ public void resetJavaRandom ( ) {
46
+ javaRandom = new PythonRandom () ;
33
47
}
34
48
}
Original file line number Diff line number Diff line change 2
2
3
3
import java .math .BigInteger ;
4
4
import java .util .List ;
5
- import java .util .Random ;
6
5
7
6
import com .oracle .graal .python .builtins .Builtin ;
8
7
import com .oracle .graal .python .builtins .CoreFunctions ;
@@ -73,7 +72,8 @@ public PNone setstate(PRandom random, PTuple tuple) {
73
72
if (arr .length == 1 ) {
74
73
Object object = arr [0 ];
75
74
if (object instanceof Long ) {
76
- random .setJavaRandom (new Random ((Long ) object ));
75
+ random .resetJavaRandom ();
76
+ random .setSeed ((Long ) object );
77
77
return PNone .NONE ;
78
78
}
79
79
}
@@ -88,7 +88,7 @@ public abstract static class GetStateNode extends PythonBuiltinNode {
88
88
@ Specialization
89
89
@ TruffleBoundary
90
90
public PTuple getstate (PRandom random ) {
91
- return factory ().createTuple (new Object []{random .nextLong ()});
91
+ return factory ().createTuple (new Object []{random .getSeed ()});
92
92
}
93
93
}
94
94
You can’t perform that action at this time.
0 commit comments