jeudi 25 juin 2020

Java<-->Python pseudorandom numbers consistency; is it possible?

I was wondering if I could set a seed in my Java program (for training/inference on Neural Networks) and my Python program (again for the same purpose) which would give me the same set of pseudorandom outputs everytime.

For example, this is a Java program,

import java.util.Random;

public class MyClass {
    public static void main(String args[]) {
        Random r = new Random(); 
        // setting seed 
        long s = 30; 
        r.setSeed(s); 
  
        // value after setting seed 
        System.out.println(r.nextInt(10));
        
    }
    
}

and a corresponding Python program,

import random

random.seed(30)
print (random.randint(0,10))

Of course, the outputs of both the programs were different (because probably Java and Python both use different pseudo random generators). In that case, is it possible for me to force Java and Python to use the same sequence of pseudorandom numbers or pseudorandom generators?

I would need this to have consistency in writing ML programs in Java/Python which give me the same output. Any help appreciated, thanks!




Aucun commentaire:

Enregistrer un commentaire