Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Math.random VS random.nextDouble

 
Ronnie Phelps
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the advantages, if any, of using the Random class to return a random number over using the Math class?
 
Chad McGowan
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the last couple of posts in this link. I tested both and found the Random class to be faster.
http://www.javaranch.com/ubb/Forum34/HTML/001112.html
 
Daniel Searson
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From memory, I have looked at the source code for the Math class and seen that it uses a Random object to produce its values. This may be wrong in different versions of Java though.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic