1

I have a very strange bug that I am trying to fix. I will try my best to explain it.

I have a class, and in that class I have a method that picks a random number. The random number generation has to generate the exact same sequence of numbers every time I run the application (different numbers, but appear in the same order no matter when I run the app).

Therefore I need to use seeds.

My code looks something like this:

    public class Tree{
    Random rand = new Random(12345);

///...code...///

         public Tree randomNode() {

              //HERE IS THE ERROR
              int r = rand.nextInt(this.size);

              if (r == 0) {
                 return this;

              } 
              else if (left != null && 1 <= r && r <= left.size) {
                 return left.randomNode();
              } 
              else {
                 return right.randomNode();
              }
           }
        }

I have a helper class that uses this, it looks a bit like this:

   public Crossover(Tree p1, Tree p2)
   {
      Tree subtreeP1 = p1.randomNode();
      Tree subtreeP2 = p2.randomNode();
   }

My main method looks a bit like this:

for(i=0;i<cross;i++)
{
Crossover c = new Crossover(p1,p2);
}

During the first loop in the main method, the numbers for r generate randomly and are fine. However, on the 2nd loop (and all that follow) is where I encounter the problem. The problem is on the line int r = rand.nextInt(this.size); - After the 2nd loop I ALWAYS get 0 no matter what.

If I remove the seed, all works well. If I include the seed, I start getting 0 ALWAYS after the first loop.

How can I continue using a seed, but get random numbers? And what am I doing wrong?

**EDIT: **

this.size refers to the size of my tree. I have ensured it is NOT zero when I get r to be 0. ALSO I would like to point out, that when I simply remove the number seed, it works fine, and random number generation works fine. When I add the seed, I have the problem.

EDIT #2 Ok, so as requested, I've put together a little program that illustrates my problem. Just run it as is. Once with the seed and once without.

import java.util.*;
public class Problem{

   public static void main(String args[])
   {
      for(int i=0; i<100; i++)
      {
         Clas c1 = new Clas();
         Helper h = new Helper(c1);
         System.out.println(h.getR());
      }
   }
}
class Helper
{
   int r;
   Helper(Clas c)
   {
      r = c.method();
   }
   public int getR()
   {
      return r;
   }      
}
class Clas
{

   Random rand = new Random(1825897);
   Clas()
   {
   }
   public int method()
   {
      int r = rand.nextInt(10); 
      return r;
   }

}
RK2015
  • 397
  • 1
  • 3
  • 15
  • 5
    What is `this.size`? Please provide a short but complete program demonstrating the problem. My guess is that with the seed you've got, you happen to end up with a tree of size 1. – Jon Skeet Aug 27 '15 at 21:15
  • 1
    Maybe `this.size` is set to 1 after the first loop, causing `nextInt` to always result 0. – Tunaki Aug 27 '15 at 21:16
  • A lot of code is missing to see what might be going on. Try debugging the code. – Andreas Aug 27 '15 at 21:22
  • my program is very long and complicated. I tried to just take out relevant bits which are used when the error arises – RK2015 Aug 27 '15 at 21:27
  • @Tunaki: That was my first thought, but it wouldn't explain why it works when he removes the seed value. – StriplingWarrior Aug 27 '15 at 21:34
  • possible duplicate of [Java random numbers not random?](http://stackoverflow.com/questions/16290026/java-random-numbers-not-random) – Larry Shatzer Aug 27 '15 at 21:36
  • @LarryShatzer That does not solve my problem, as I need the SAME seed each time – RK2015 Aug 27 '15 at 21:43
  • I've edited my question with an example illustrating my problem – RK2015 Aug 27 '15 at 21:44
  • I ran the sample code you added: the code always outputs 3 when the seed is used. What is the problem in this code? This is normal since the `Random` object is recreated in each loop. – Tunaki Aug 27 '15 at 21:46
  • Are you recreating your `Random` before each iteration? You need to do that, otherwise your sequence will not restart after the first iteration. – Aivean Aug 27 '15 at 21:47
  • @Tunaki My question is, in the most simplest way, is how to generate different numbers but will be the same each time I run the application. So Run #1: 1, 4, 6, 8, 0, 4, Run #2: 1, 4, 6, 8, 0, 4, Run #3: 1, 4, 6, 8, 0, 4. – RK2015 Aug 27 '15 at 21:51
  • @Aivean maybe thats my problem. Could you suggest the most simplest way it can be done? – RK2015 Aug 27 '15 at 21:52

2 Answers2

2

In the loop, you don't want to reset the Random Number Generator, I assume?

In the example, you could do:

 Clas c1 = new Clas();
 for(int i=0; i<100; i++)
 {
     Helper h = new Helper(c1);
     System.out.println(h.getR());
 }

In your original example, you seem to have multiple Tree instances, so you can't use this. But you can declare your Random static, so it is shared among all Trees:

static Random rand = new Random(1825897);
GeertPt
  • 16,398
  • 2
  • 37
  • 61
-2

Try using the function Math.random() to generate your random number. Math.random() generates a random number between 0 and 1.

Hassan Al Bourji
  • 185
  • 1
  • 13