Rolling The Dice using random function - Java Language Basics

Java examples for Language Basics:Random

Description

Rolling The Dice using random function

Demo Code

public class DiceApp
{
  public static void main(String[] args)
  {// ww w .ja  v  a 2 s .  com
    int roll;
    String msg = "Here are 100 random rolls of the dice:";
    System.out.println(msg);
    for (int i=0; i<100; i++)
    {
      roll = randomInt(1, 6);
      System.out.print(roll + " ");
    }
    System.out.println();
  }

  public static int randomInt(int low, int high)
  {
    int result = (int)(Math.random()
        * (high - low + 1)) + low;
    return result;
  }

}

Related Tutorials