Java Randomness
Post MTG Forge Related Programming Questions Here
Moderators: timmermac, Agetian, friarsol, Blacksmith, KrazyTheFox, CCGHQ Admins
6 posts
• Page 1 of 1
Java Randomness
by mark » 18 May 2013, 15:27
I played a lot with forge and something told me that the shuffling was always a little bit weird, e.g. much periodic behaviour.
As I looked at the source code of Cockatrice, I saw that they use a SIMD-oriented Fast Mersenne Twister for random numbers (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/) and asked me why... then I dug deeper and found that several languages use Mersenne-Twister as RNG, like Python, Ruby, R, PHP, MATLAB and C++11 but Java does not... multiple sources told me that the java.util.Random class is not very state of the art, see http://www.alife.co.uk/nonrandom/ and http://www.qbrundage.com/michaelb/pubs/ ... ation.html
The second source has Mersenne Twister and R250/521 implementations for Java under public domain and shortly describes them.
Could you improve the forge.util.MyRandom wrapper code by copying the MT class over and use it instead of using java.util.Random? It's only copy-paste with three lines of changes.
As I looked at the source code of Cockatrice, I saw that they use a SIMD-oriented Fast Mersenne Twister for random numbers (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/) and asked me why... then I dug deeper and found that several languages use Mersenne-Twister as RNG, like Python, Ruby, R, PHP, MATLAB and C++11 but Java does not... multiple sources told me that the java.util.Random class is not very state of the art, see http://www.alife.co.uk/nonrandom/ and http://www.qbrundage.com/michaelb/pubs/ ... ation.html
The second source has Mersenne Twister and R250/521 implementations for Java under public domain and shortly describes them.
Could you improve the forge.util.MyRandom wrapper code by copying the MT class over and use it instead of using java.util.Random? It's only copy-paste with three lines of changes.
Re: Java Randomness
by mark » 18 May 2013, 19:32
ok, it looks like there are more changes needed (102 occurences in 77 files) for this because the code directly uses random-instances everywhere which are used to generate int or float numbers.
The wrapper class basically only saves a 'new Random()'-statement instead of capsulating this and returning an int or float.
Do you plan to do this? It's cleaner, smaller, better
You could create the methods public static int getRandomInt() and getRandomFloat() for the MyRandom wrapper class, which directly return the random() value from the MT class as int or float and start using it when new code needs random numbers.
The usage of the old getRandom method can be rooted out step by step everywhere (please begin with card shuffling
).
The wrapper class basically only saves a 'new Random()'-statement instead of capsulating this and returning an int or float.
Do you plan to do this? It's cleaner, smaller, better
You could create the methods public static int getRandomInt() and getRandomFloat() for the MyRandom wrapper class, which directly return the random() value from the MT class as int or float and start using it when new code needs random numbers.
The usage of the old getRandom method can be rooted out step by step everywhere (please begin with card shuffling
Re: Java Randomness
by Max mtg » 18 May 2013, 19:40
You won't be able to completelly root out java.util.Random usages.
Given such a class it can be attached to Forge in no time.
- Code: Select all
final List<CardPrinted> shuffled = deck.toFlatList();
Collections.shuffle(shuffled, MyRandom.getRandom());
Given such a class it can be attached to Forge in no time.
Single class for single responsibility.
- Max mtg
- Programmer
- Posts: 1997
- Joined: 02 Jul 2011, 14:26
- Has thanked: 173 times
- Been thanked: 334 times
Re: Java Randomness
by mark » 18 May 2013, 19:44
http://stackoverflow.com/questions/2249 ... doing-what shows Java's source code for Collection shuffling, I guess it should be possible to create a new Collection class that inherits everything but overrides shuffling.
Edit: yeah, overriding the Random class should be better
Edit: yeah, overriding the Random class should be better
Re: Java Randomness
by Max mtg » 18 May 2013, 20:01
This just won't happen.mark wrote:I guess it should be possible to create a new Collection class that inherits everything but overrides shuffling.
By the way there's an implementation alredy done in Java 6mark wrote:Edit: yeah, overriding the Random class should be better
http://docs.oracle.com/javase/6/docs/ap ... andom.html
Single class for single responsibility.
- Max mtg
- Programmer
- Posts: 1997
- Joined: 02 Jul 2011, 14:26
- Has thanked: 173 times
- Been thanked: 334 times
Re: Java Randomness
by mark » 19 May 2013, 07:40
The SecureRandom class is much better, but up to 60 times slower [1]. Nevertheless, it simply works by applying the following patch to forge without noticeable performance impacts:
- Code: Select all
Index: src/main/java/forge/util/MyRandom.java
===================================================================
--- src/main/java/forge/util/MyRandom.java (Revision 21526)
+++ src/main/java/forge/util/MyRandom.java (Arbeitskopie)
@@ -18,6 +18,7 @@
package forge.util;
import java.util.Random;
+import java.security.SecureRandom;
/**
* <p>
@@ -30,7 +31,7 @@
*/
public class MyRandom {
/** Constant <code>random</code>. */
- private static Random random = new Random();
+ private static Random random = new SecureRandom();
/**
* <p>
6 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 16 guests