Shifted and scaled random integers. - Java Language Basics

Java examples for Language Basics:Random

Description

Shifted and scaled random integers.

Demo Code

import java.security.SecureRandom; // program uses class SecureRandom

public class Main 
{
   public static void main(String[] args)
   {      /*w w w.  j a  v  a2 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();
      } 
   } 
}

Result


Related Tutorials