Random value range
In this chapter you will learn:
Random double value between 0 and 1
Call nextGaussian() to get next double value centered at 0.0 with a standard deviation of 1.0.
import java.util.Random;
/*from j a v a 2 s.com*/
public class MainClass {
public static void main(String args[]) {
Random generator = new Random();
for(int i = 0; i < 10; i++) {
System.out.println(generator.nextGaussian());
}
}
}
The code above generates the following result.
Random int value in a range
import java.util.Random;
/* j a v a 2s. co m*/
public class Main {
public static void main(String[] args) {
Random r = new Random();
int n = r.nextInt(3);
System.out.println(n);
n = r.nextInt(3);
System.out.println(n);
n = r.nextInt(3);
System.out.println(n);
n = r.nextInt(3);
System.out.println(n);
n = r.nextInt(3);
System.out.println(n);
n = r.nextInt(3);
System.out.println(n);
n = r.nextInt(3);
System.out.println(n);
n = r.nextInt(3);
System.out.println(n);
n = r.nextInt(3);
System.out.println(n);
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Utility Classes