Random value range

In this chapter you will learn:

  1. How to generate random value between 0 and 1
  2. How to generate random value in a range

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:

  1. How to compile Java source code dynamically
  2. How to compile and run a Java source dynamically
Home » Java Tutorial » Utility Classes
Java standard stream
Java system property get and set
Current time in millis second and nano second
Random UUID
JVM memory
JVM garbage collector
JVM shutting down
Processor count
OS system commands
Random class
Random value
Random value range
Compile Java source code
Timer and TimerTask