Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 142
  • Last Modified:

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
0
tinybear
Asked:
tinybear
  • 2
  • 2
1 Solution
 
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
 
Ajay-SinghCommented:
> 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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now