problem with Random Number genreation code

Ok can anyone tell me what is wrong with the following?

I want to create a random number gen. and be able to reseed it as required.

I get syntax errors in Netbeans 5.5
tinybearAsked:
Who is Participating?
 
Ajay-SinghConnect With a Mentor Commented:
> Random generator = new Random(seed);

 
You need to define this as field of the class
 
 
public class RandomNumber {
        Random generator;
   
    /** Creates a new instance of RandomNumber */
    public RandomNumber() {
     
        long d = (long) new Date().getTime();
        long seed = d;
        generator = new Random(seed);
    }
0
 
Ajay-SinghCommented:
perhaps you are not importing java.util.Random. Can you post the code?
0
 
tinybearAuthor Commented:
import java.math.*;
import java.util.Random;
import java.util.Date;

       
/**
 *
 * @author Kevin Timmins
 */
public class RandomNumber {
   
    /** Creates a new instance of RandomNumber */
    public RandomNumber() {
     
        long d = (long) new Date().getTime();
        long seed = d;
        Random generator = new Random(seed);
       
 
    }
   
   public int getNextNum(int n){
       int myNum =  generator.nextInt(n);
       
       return myNum;
       
   }
   
   public void newRandom(){
       long d = (long) new Date().getTime();
       generator.setSeed(d);
       
       
   }

   
}
0
 
tinybearAuthor Commented:
Im getting errors @

   public int getNextNum(int n){

/*here-->*/

       int myNum =  generator.nextInt(n);
       
       return myNum;
       
   }
   
   public void newRandom(){
       long d = (long) new Date().getTime();

/* here-->*/

       generator.setSeed(d);
       
       
   }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.