Monday, January 6, 2014

How to generate random numbers.



// The following program will generate 
// random number like the numbers gained by throwing a dice.
class dice
{
  public static void main(String args[])
  {
    int i,n;
    double r;
    for(i=1;i<=10;i++)
    {
      r=Math.random();
      n=(int)(r*6)+1;
      System.out.println(n);
    }
  }
}

The following illustration show how to write code for generating random numbers starting from 17 to 20. This mean the generated random numbers will be 17, 18, 19, 20.

No comments:

Post a Comment