Creating random numbers - Java Language Basics

Java examples for Language Basics:Random

Introduction

The following code shows how to do this, with the values set to 1 and 6 for a dice-playing game:

Demo Code

public class Main {
  public static void main(String[] args) {
    int low = 1;      // the lowest value in the range
    int high = 6;     // the highest value in the range
    int rnd = (int)(Math.random() * (high - low + 1)) + low;

    System.out.println(rnd);//from w  ww.ja v  a  2  s.c  o m

  }

}

Related Tutorials