2

I'm trying to generate random numbers in Python code running on the JVM with PyBee's VOC transpiler. For now, it looks like the Python random module isn't included, so how can I generate random numbers?

Here's the code I tried to transpile:

from random import randrange

print(randrange(5))

And here's the error when I run the class file in Java:

Exception in thread "main" java.lang.ClassCastException: org.python.java.Module (in module: Unnamed Module) cannot be cast to org.python.Callable (in module: Unnamed Module)
    at python.example.module$import(example.py:3)
    at python.example.main(example.py)
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286

1 Answers1

3

It looks like my Python code can access all the standard Java libraries, so I just have to emulate the random module with Java.

from java.util import Random

r = Random()
randrange = r.nextInt

print(randrange(5))

If I want to test my code on Python before deploying to the JVM or Android, I can pass in the randrange() function as a parameter.

Don Kirkby
  • 53,582
  • 27
  • 205
  • 286