Reputation: 21
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.
Upvotes: 1
Views: 1295
Reputation: 50716
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.
Upvotes: 1
Reputation: 36107
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
Upvotes: 0
Reputation: 34189
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()
}
}
Upvotes: 1