Generate random numbers using Math.random

  1. /*
  2.   Generate random numbers using Math.random
  3.   This java example shows how to generate random numbers using random method of
  4.   Java Math class.
  5. */
  6.  
  7. public class GenerateRandomNumbers {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. /*
  12.   * To generate random numbers, use
  13.   * static double random() method of Java Math class.
  14.   *
  15.   * This method returns a positive double value grater than 0.0
  16.   * and less than 1.0
  17.   */
  18.  
  19. System.out.println("Random numbers between 0.0 and 1.0 are,");
  20. for(int i=0; i < 5 ; i++)
  21. System.out.println("Random Number ["+ (i+1) + "] : " + Math.random());
  22.  
  23. /*
  24.   * To generate random number between 1 to 100 use following code
  25.   */
  26.  
  27. System.out.println("Random numbers between 1 and 100 are,");
  28. for(int i=0; i < 5 ; i++)
  29. System.out.println("Random Number ["+ (i+1) + "] : " + (int)(Math.random()*100));
  30.  
  31. }
  32. }
  33.  
  34. /*
  35. Typical output would be
  36. Random numbers between 0.0 and 1.0 are,
  37. Random Number [1] : 0.7900395454653136
  38. Random Number [2] : 0.15887365598103076
  39. Random Number [3] : 0.5570570713930629
  40. Random Number [4] : 0.017811004461356195
  41. Random Number [5] : 0.7135560403213513
  42.  
  43. Random numbers between 1 and 100 are,
  44. Random Number [1] : 31
  45. Random Number [2] : 21
  46. Random Number [3] : 24
  47. Random Number [4] : 95
  48. Random Number [5] : 3
  49. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

It didnt' work out for mw

Hi,
i am tryigb to figuer out the Math.random class, ti randomly creates numbers
of type Double, right? so if i want to create numbers between 0-100, your second method should work, isn't it?
however, i copy paste your code the IDE, and it kept do give me the followign exceptions:

at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Exception in thread "main"

Thanks, Gil

How to generate a random int (sorry I typed it in my Java IDE)

  1. //As you might know, Math.random() generates a random double where X<0
  2. //To generate a random int using the same method (ie Math.random()) you do the //following:
  3.  
  4. public class RandomInt{
  5. public static void main(String[] args){
  6. int random = (int)(Math.random()*10)+5; //*see bottom
  7. System.out.println(random);
  8. }
  9. }
  10. //the "(int)" parses the double to an int value
  11. //replace 10 with your range of numbers
  12. //(ie if you want a number between 5 and 15 the range is 10 [15-5]
  13. //replace the 5 with the staring number
  14. //(ie if you want the lowest number in the range to be 5 then add 5)
  15. //another eg.
  16. //int ran = (int)(Math.random()*100)-50;
  17. //will return a value in the range [-50;50]

Question

Can anyone tell me what this code does and what it means

System.out.println((int)(6*Math.random())+1);

Don't misunderstand

It means generate a random number from the six consecutive numbers starting from 1. That means any number from 1,2,3,4,5,6. It does NOT mean any number from 1 to 6.

How about this code?

  1. System.out.println((int)(6*Math.random())+4);

Will it give you random number from range 4-6? NO! It will give you random numbers from range 4-9! Reason: The six consecutive numbers starting from 4 are 4,5,6,7,8,9.

If you want random numbers from 4-6, you must use this code:

  1. System.out.println((int)( (6-4+1) *Math.random())+4);

OR
  1. System.out.println((int)(3*Math.random())+4);

It is good

It is good


Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!