
org.python.modules.random.PyRandom Maven / Gradle / Ivy
Go to download
Jython is an implementation of the high-level, dynamic, object-oriented
language Python written in 100% Pure Java, and seamlessly integrated with
the Java platform. It thus allows you to run Python on any Java platform.
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.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
@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 PyObject Random_seed(PyObject seed) {
if (seed == null) {
seed = new PyLong(System.currentTimeMillis());
}
if (seed instanceof PyLong) {
PyLong max = new PyLong(Long.MAX_VALUE);
PyLong seed_modulus = (PyLong)(seed.__mod__(max));
this.javaRandom.setSeed(seed_modulus.asLong(0));
} else if (seed instanceof PyInteger) {
this.javaRandom.setSeed(((PyInteger)seed).getValue());
} else {
this.javaRandom.setSeed(seed.hashCode());
}
// duplicating the _randommodule.c::init_by_array return
return Py.None;
}
@ExposedNew
@ExposedMethod
public void Random___init__(PyObject[] args, String[] keywords) {}
@ExposedMethod
public PyObject Random_jumpahead(PyObject arg0) {
long inc;
if (arg0 instanceof PyLong) {
inc=((PyLong)arg0).asLong(0);
} else if (arg0 instanceof PyInteger) {
inc=((PyInteger)arg0).getValue();
} else {
throw Py.TypeError("jumpahead requires an integer");
}
for(int i=0;i>>5;
long b=this.javaRandom.nextInt()>>>6;
double ret=(a*67108864.0+b)*(1.0/9007199254740992.0);
return new PyFloat(ret);
}
@ExposedMethod
public PyLong Random_getrandbits(int k) {
return new PyLong(new BigInteger(k, javaRandom));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy