An example of Math.random()

Home arrow Java Tutorials arrow Source code arrow An example of Math.random()
An example of Math.random() Print E-mail
Contributed by Howell   
Monday, 12 June 2006
Keywords: java Math random  tutorial 

The Math class provides the usual mathematical functions and defines the commonly used constants, e and pi, as the static double variables Math.E and Math.PI. All elements of the Math class are static, so you never create an instance of Math. The Math class is itself final and so cannot be subclassed. Here are some important points to remember about some of the more commonly used methods:

  • The trigonometric functions— The trigonometric functions are concerned with calculations involved in geometry, such as lines and angles. The methods take double primitive arguments and return double results. Angles are always expressed in radians.
  • The max, min, and abs functions— These methods (for minimum, maximum, and absolute value) are overloaded, with separate versions for int, long, float, and double arguments. Remember that integer inputs that are smaller than 32 bits are automatically promoted to int.
  • The ceil (ceiling) and floor methods— These take double inputs and return double values that represent the integers above and below the input, respectively.
  • The round method— This is overloaded, with versions for float and double inputs that return the closest int and long, respectively.
  • The sqrt (square root) function— This takes a double as an argument and returns a double value. Note that if the input is negative, the result is the special NaN (Not a Number) value.
  • The random function— This method returns a double primitive randomly distributed between 0.0 and 1.0.
  • The pow method— This method takes two values as arguments and returns the value of the first argument raised to the power of the second argument
     

Math.random() returns a random number between 0.0 and 1.0.

Unlike some random number system Java does not appear to offer the ability to pass a seed number to increase the randomness. This method can be used to produce a random number between 0 and 100 as follows.

For the purpose of the exam one of the important aspects of this method is that the value returned is between 0.0 and 1.0. Thus a typical sequence of output might be


0.9151633320773057
0.25135231957619386
0.10070205341831895

Often a program will want to produce a random number between say 0 and 10 or 0 and 100. The following code combines math code to produce a random number between 0 and 100.

        System.out.println(Math.round(Math.random()*100));
Sample:


class RandInt
  {
  static int[] setupArray(int n)
    {
    int a[] = new int[n];
    for (int i=0; i<a.length; i++)
      a[i] = (int)(Math.random()*10);
    return a;
    }
  static String showArray(int[] a)
    {
    String s = "length=" + a.length;
    for (int i=0; i<a.length; i++)
      {
      if (i%10==0)
        s = s + "\n";
      else
        s = s + a[i] + "  ";
      }
    s = s + "\n";
    return s;
    }
  public static void main(String args[])
    {
    int a[] = setupArray(100);
    System.out.println(showArray(a));
    int count[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    for (int i=0; i<a.length; i++)
      count[a[i]]++;
    for (int i=0; i<count.length; i++)
      System.out.println(i+"\t"+count[i]);
    }
Last Updated ( Wednesday, 21 June 2006 )

  home              contact us

 

©2006-2008 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground