Advertisement
MrsMcLead

Random Numbers

Oct 10th, 2013
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. import java.lang.Math;
  2.  
  3. public class RandomNumberGenerator
  4. {
  5.    public static void main(String[] args)
  6.    {
  7.        int x = 0; //x is some number that I want to assign a value to
  8.        
  9.        /*
  10.         * Math.random() returns a double value between 0 and 1.
  11.         *
  12.         * Here, I am looking for an integer value between 1 and 10.
  13.         *
  14.         * I will use Math.random() to find a value, and multiply by 10.
  15.         *
  16.         * Since this calculation will result in a double between 0 and 9, I will add 1.
  17.         *
  18.         * I will also cast my result to an int.
  19.         */
  20.        
  21.        x = (int)(Math.random()*10+1);
  22.        System.out.println(x);
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement