1
public class MazeBuilder implements Runnable { // class internal local variables protected SingleRandom random ; // random number stream, used to make randomized decisions, e.g for direction to go Order order; // describes what is wanted, e.g. a perfect maze or not /** * Constructor for a randomized maze generation */ public MazeBuilder(){ random = SingleRandom.getRandom(); } /** * Constructor with option to make maze generation deterministic or random */ public MazeBuilder(boolean deterministic){ if (true == deterministic) { this.random = random ; } random = SingleRandom.getRandom(); } 

The first constructor randomly generates a maze. I need to implement code so that if MazeBuilder.build is called for the same skill level twice, it will deliver the same results. I think "this.random = random ;" in the second constructor will do this, but I'm not sure this is correct.

4
  • 1
    What is SingleRandom? Commented Jun 10, 2016 at 3:50
  • 1
    Do you mean using the same random seed? Commented Jun 10, 2016 at 3:51
  • 2
    Usually you use the same seed. Commented Jun 10, 2016 at 3:52
  • 2
    The beauty of this question... generate the same random sequence twice Commented Jun 10, 2016 at 3:53

3 Answers 3

1

What you want to achieve is actually called random seed. If you initialize Random with the same seed, then you will get the same sequence every time.

Read more about what random seed is at Wikipedia or at StackOverflow.

You can simply do something like this:

public class MazeBuilder { private Random _random; public MazeBuilder() { _random = new Random(); } public MazeBuilder(int seed) { _random = new Random(seed); } public void generateMaze() { // here you use _random.next() } } 
Sign up to request clarification or add additional context in comments.

1 Comment

I am not sure if my code is quite correct - I am not a Java, but C# developer. Please, let me know, if there any errors :)
1

If you're using a class like this or something similar, try this:

private static final int SEED = 1234; // or any other int /** * Constructor with option to make maze generation deterministic or random */ public MazeBuilder(boolean deterministic) { random = SingleRandom.getRandom(); if (deterministic) { random.setSeed(SEED); } } 

By calling setSeed() with a hard-coded value, you'll ensure that it follows the same sequence on every run.

1 Comment

Yes! That is exactly what my class looks like and this seems correct.
0

I don't know how you generate random numbers and what SingleRandom object is
but in java there is a Random class => https://docs.oracle.com/javase/7/docs/api/java/util/Random.html
in which a setSeed method: https://docs.oracle.com/javase/7/docs/api/java/util/Random.html#setSeed(long)
works in such a way that:

it alters the state of this random number generator object so as to be in exactly the same state as if it had just been created with the argument seed as a seed.

The below code resets the random objects using setSeed, and in this way it generates twice the same stream of random numbers:

 Random rand = new Random(); rand.setSeed(3); for( int i = 1; i <= 10; i++){ System.out.println( rand.nextInt()); } System.out.println( "============="); rand.setSeed(3); for( int i = 1; i <= 10; i++){ System.out.println( rand.nextInt()); } -1155099828 -1879439976 304908421 -836442134 288278256 -1795872892 -995758198 -1824734168 976394918 -634239373 ============= -1155099828 -1879439976 304908421 -836442134 288278256 -1795872892 -995758198 -1824734168 976394918 -634239373 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.