It is currently 29 Oct 2025, 08:58
   
Text Size

Java Randomness

Post MTG Forge Related Programming Questions Here

Moderators: timmermac, Agetian, friarsol, Blacksmith, KrazyTheFox, CCGHQ Admins

Java Randomness

Postby 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.
mark
 
Posts: 138
Joined: 28 Dec 2011, 11:32
Has thanked: 6 times
Been thanked: 11 times

Re: Java Randomness

Postby 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 :mrgreen:
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 :mrgreen:).
mark
 
Posts: 138
Joined: 28 Dec 2011, 11:32
Has thanked: 6 times
Been thanked: 11 times

Re: Java Randomness

Postby Max mtg » 18 May 2013, 19:40

You won't be able to completelly root out java.util.Random usages.
Code: Select all
        final List<CardPrinted> shuffled = deck.toFlatList();
        Collections.shuffle(shuffled, MyRandom.getRandom());
But you can always create a derived class from java.util.Random that would feature a better algorithm.

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

Postby 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
mark
 
Posts: 138
Joined: 28 Dec 2011, 11:32
Has thanked: 6 times
Been thanked: 11 times

Re: Java Randomness

Postby Max mtg » 18 May 2013, 20:01

mark wrote:I guess it should be possible to create a new Collection class that inherits everything but overrides shuffling.
This just won't happen.

mark wrote:Edit: yeah, overriding the Random class should be better
By the way there's an implementation alredy done in Java 6
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

Postby 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>
If this slows down too much, there is always the option to change to MT.
mark
 
Posts: 138
Joined: 28 Dec 2011, 11:32
Has thanked: 6 times
Been thanked: 11 times


Return to Developer's Corner

Who is online

Users browsing this forum: No registered users and 20 guests

Main Menu

User Menu

Our Partners


Who is online

In total there are 20 users online :: 0 registered, 0 hidden and 20 guests (based on users active over the past 10 minutes)
Most users ever online was 9298 on 10 Oct 2025, 12:54

Users browsing this forum: No registered users and 20 guests

Login Form