• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Random Class

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you have math.random() method to generate random numbers, why we still have a Random class? Isn't it inefficient?
------------------
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Iqbal
The java.lang.Math class has only the one method for generating randon numbers (<code>random()</code>), which returns a double value greater than or equal to 0.0 and less than 1.0. Generating random numbers is only a small part of the overall functionality it affords the programmer.
As you have rightly noticed, The Java.util.Random class can do the same thing, via the <code>nextDouble()</code> method. However, the Random class also provides further methods to generate random booleans, floats, ints, bytes, ints between a range specified by the user, and various other bits and pieces. It is therefore a specialised class dealing only with the generation of random values.
If you only need to generate a random double, then it is cheaper and easier to use the static method of the Math class. However, if you wanted to generate a series of random values of different types, then creating and using an instance of Random is a convenient way to do it.

Hope this helps.

------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic