10

Is it possible to generate the same random number in python as in java if given the same seed

Explained in a more accurate way, Is it possible to use the linear congruential formula from java in Python?

4
  • 1
    If you could use Jython, I imagine you could hook into Java's random stuff directly and use it, doing this. Commented Jun 22, 2012 at 19:25
  • I would prefer to use pure python (from python.org's website) and somehow importing the java generator, if at all possible. Commented Jun 22, 2012 at 19:28
  • 1
    Anything that you have the formula for, you can code. I doubt you'll find something pre-built. Commented Jun 22, 2012 at 19:30
  • You mean CPython - nothing purer about it, just the reference implementation. Commented Jun 22, 2012 at 19:30

2 Answers 2

10

Have you taken a look at this project? http://pypi.python.org/pypi/java-random

4
  • 3
    And once again Python makes me eat my words. Referring to my comment on the question. Commented Jun 22, 2012 at 19:31
  • This seems to do it, they even guessed what i needed it for. And it's working, thanks Commented Jun 22, 2012 at 19:38
  • 1
    @DelusionalLogic Generally an underscore means an implementation detail, so it's not something you want to access. There is a function to set the seed. Commented Jun 22, 2012 at 19:41
  • @Lattyware It was just repl taht didn't like it. It's working now that i created a real script for it. Commented Jun 22, 2012 at 19:42
1

Using Pyjnius:

from jnius import autoclass

rand = autoclass('java.util.Random')()

rand.setSeed(12345)

print(rand.nextInt(50))
print(rand.nextInt(50))
print(rand.nextInt(50))
print(rand.nextInt(50))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.