How to Generate Random Number in Java



How to Generate Random Number in Java

Sample Code

import java.util.Scanner;

public class randomnumber {
  public static void main(String[] args) 
  {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number of random number need: ");
    int n = sc.nextInt();
    for (int i = 0; i < n; i++) 
    {
      System.out.println(i + " Random Number: " + Math.random());
    }
  }
}

Output

Enter the number of random number need: 4
0 Random Number: 0.9182131936022618
1 Random Number: 0.3220397825133039
2 Random Number: 0.6498860460747687
3 Random Number: 0.001956330809479323

Related Searches to How to Generate Random Number in Java