Java SecureRandom generate random integer

Description

Java SecureRandom generate random integer


import java.security.SecureRandom; 

public class Main 
{
   public static void main(String[] args)
   {      //  w  w w  .j  a  v  a  2  s . c o m
      // randomNumbers object will produce secure random numbers
      SecureRandom randomNumbers = new SecureRandom(); 

      // loop 20 times
      for (int counter = 1; counter <= 20; counter++) 
      {
         // pick random integer from 1 to 6
         int face = 1 + randomNumbers.nextInt(6);

         System.out.printf("%d  ", face); // display generated value
         
         // if counter is divisible by 5, start a new line of output
         if (counter % 5 == 0)
            System.out.println();
      } 
   } 
}



PreviousNext

Related