Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

diff. bet. Math.random() and java.util.random

 
Augustine Kanagasabapathy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
Could any one tell me whether any practical diff. bet the Math.random and java.util.Random class. I understand that Math.random() is a static method & it is shared by all who r accessing it. But using java.util.Random, i can create new instance every time. any other difference???
thanx in adv.
luv,
Augg
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked the source code, and the "Math.random()" method does a lazy (on-demand) instantiation of a "java.util.Random" instance the first time it's invoked. Then it simply calls the "nextDouble()" method. So Math.random() is simply a wrapper around "java.util.Random".
 
Augustine Kanagasabapathy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanx for ue reply. I do not understand what u mean by the word on-demand instantiation. When i say "new A()", then JVM has to create a new instance of A, right??? Could u pls explain this in detail???
luv,
Augg
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He just means the instance of Random isn't created until the first time Math.random() is called; like
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic