-1

I'm trying to generate a random between 1 and 100 in NetBeans, but what I used before in Eclipse isn't working. I can't seem to use Random, as it is underined in red: "cannot find symbol." Please show me how.

Random x = new Random();
int n = x.nextInt(100);//random number 1-100
Ben
  • 321
  • 1
  • 6
  • 21

4 Answers4

1

Either use the fully qualified class name (or add an import). The import might look something like,

import java.util.Random;

while the fully qualified class name is java.util.Random like

java.util.Random x = new java.util.Random();

Also, for a number in the range 1 - 100 you need

// int n = x.nextInt(100);//random number 1-100
int n = 1 + x.nextInt(100);

Because nextInt(int) (per the Javadoc)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Most likely you are missing the import:

import java.util.Random;
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

For all errors like "cannot find symbol.", you can quickly hit Ctrl + Shift + I to import all missing library (in Eclipse it is Ctrl + Shift + O). You also should search on the Internet for this error first, the answer everywhere.

1

This is how to make it generate a random number, I know it is longer but its much easier to understand.

import java.util.Random;
class (INSERTCLASSNAME){
    public static void main(String[] args){
        Random random = new Random();
        int number;
        for(int counter=1; counter<=1;counter++){
        number = 1+random.nextInt(100);
        System.out.println(number);
        }
    }
}