Mega Code Archive

 
Categories / Java / Development Class
 

Does Math random() produce 0 0 and 1 0

//: c03:RandomBounds.java // Does Math.random() produce 0.0 and 1.0? // {RunByHand} // From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002 // www.BruceEckel.com. See copyright notice in CopyRight.txt. public class RandomBounds {   static void usage() {     System.out.println("Usage: \n\t" +       "RandomBounds lower\n\tRandomBounds upper");     System.exit(1);   }   public static void main(String[] args) {     if(args.length != 1) usage();     if(args[0].equals("lower")) {       while(Math.random() != 0.0)         ; // Keep trying       System.out.println("Produced 0.0!");     }     else if(args[0].equals("upper")) {       while(Math.random() != 1.0)         ; // Keep trying       System.out.println("Produced 1.0!");     }     else       usage();   } } ///:~