Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
RL4J - Changed to use Nd4j Random instead of java.util.Random #8282
What changes were proposed in this pull request?
Several places in RL4J uses java.util.Random and does not use the seed. This makes it impossible to have identical results with identical runs. Plus java.util.Random makes it very difficult to unit test components that uses random.
How was this patch tested?
Unit tests and manual tests
The following checklist helps ensure your PR is complete:
Nd4j.getRandom() put and/or get the instance from a ThreadLocal storage. I was afraid that if Nd4j.getRandom().setSeed(...) is called multiple times, a user might think he is setting the seed on different Random instances but in fact is re-setting the seed on the same instance. This might cause unwanted patterns in the random sequences. This can be solved easily if we just create a new Random with every instance.
On the other hand, I could make sure the seed is only set once and use Nd4j.getRandom(0 everywhere.
Throughout DL4J and SameDiff, we have tended to use the thread local random (Nd4j.getRandom()) pretty much everywhere, which works well enough most of the time.
The problem with using getNewRandomInstance is that anything internally to the network won't be using it that random instance.
I decided to use
So these are the specs I implemented: