22
33import java .io .IOException ;
44import java .io .StringReader ;
5+ import java .security .NoSuchAlgorithmException ;
6+ import java .security .SecureRandom ;
57import java .util .List ;
68import java .util .Properties ;
9+ import java .util .Random ;
710import java .util .Scanner ;
811import java .util .Map .Entry ;
912import java .util .concurrent .ThreadLocalRandom ;
2124@ Singleton
2225public final class MultiplayerGameManager <T extends AbstractMultiplayerPlayer > extends GameManager <T > {
2326
27+ private static final String RANDOM_SECURE_SHA1PRNG_ALGORITHM = "SHA1PRNG" ;
28+
2429 private Properties gameParameters ;
2530 private long seed ;
31+ private Random random ;
2632
2733 @ Override
2834 protected void readGameProperties (InputCommand iCmd , Scanner s ) {
@@ -46,6 +52,15 @@ protected void readGameProperties(InputCommand iCmd, Scanner s) {
4652 }
4753 }
4854 gameParameters .setProperty ("seed" , String .valueOf (seed ));
55+
56+ try {
57+ // This random generator is reproducible whereas default one (native PRNG is not)
58+ random = SecureRandom .getInstance (RANDOM_SECURE_SHA1PRNG_ALGORITHM );
59+ random .setSeed (seed );
60+ } catch (NoSuchAlgorithmException e1 ) {
61+ log .error ("Error while creating secure random number generator. Default to non-secure random generator" , e1 );
62+ random = new Random (seed );
63+ }
4964 }
5065
5166 @ Override
@@ -70,15 +85,29 @@ public int getPlayerCount() {
7085
7186 /**
7287 * <p>
73- * The seed is used to generated parameters such as width and height.<br>
74- * If a seed is present in the given input, the input value should override the generated values.
88+ * The seed is used to initialize the Random number generator.<br>
89+ * If a seed is present in the given input, the input value should override the generated values.<br>
90+ *
91+ * The seed should NOT be used directly in referee but through the random number generator provided by @method getRandom
7592 * </p>
7693 *
7794 * @return an <code>long</code> containing a given or generated seed.
7895 */
7996 public long getSeed () {
8097 return seed ;
8198 }
99+
100+ /**
101+ * <p>
102+ * The random generator is used to generated parameters such as width and height.<br>
103+ * The provided random generator is a SecureRandom using the SHAPRNG algorithm.<br>
104+ * </p>
105+ *
106+ * @return an <code>Random</code> containing a given or generated seed.
107+ */
108+ public Random getRandom () {
109+ return random ;
110+ }
82111
83112 /**
84113 * <p>
0 commit comments