The program as written is strange. Takes 2304 tries to get to 0.2 delta.
The printed numbers are very uniform ;o(

This works for me:

import java.util.Random;

public class P1 {
  public static void main(String[] args) {
    Random rnd = new Random();//0);

    double last = rnd.nextDouble();
    System.out.println("Last ="+last);
    long i = 1;
    for(; i < Long.MAX_VALUE; i++) {
      //rnd.setSeed(i); //
      double next = rnd.nextDouble();
      System.out.println(next);
      if (Math.abs(next - last) > 0.2)
        break;
    }
    System.out.println(i);
  }
}


I think Random() actually initializes the seed to
 a value based on the current time, which is better than intializing with
a number
BTW, setseed() uses only 48 bits of the given number
Cheers
<k/>

On 2/19/11 Sat Feb 19, 11, "Lance Norskog" <[email protected]> wrote:

>What does this program print?
>
>
>package hack;
>
>import java.util.Random;
>
>public class JDKRandomThreatOrMenace {
>    
>    public static void main(String[] args) {
>        Random rnd = new Random(0);
>        
>        double last = rnd.nextDouble();
>        long i = 1;
>        for(; i < Long.MAX_VALUE; i++) {
>            rnd.setSeed(i);
>            double next = rnd.nextDouble();
>            if (Math.abs(next - last) > 0.2)
>                break;
>        }
>        System.out.println(i);
>    }
>
>}
>
>Your prize if you get it right? A patch would be welcome...
>


Reply via email to