aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes random( ) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "random( )" Watch "random( )" New topic
Author

random( )

catchbollu
Greenhorn

Joined: Jul 27, 2000
Posts: 5
Math class has a function random that is : -
static synchronized double random()
i want to know why is it declared static and synchronized,
what is static and synchronized doing in function random???


catchbollu entered in
sunil choudhary
Ranch Hand

Joined: Nov 10, 2000
Posts: 138
hi catch you hve a strange name ??
anyway
Almost al of the methods in MAth calss are static as we need to use them on the fly. Declaring them static makes them universally available.
Why are they syncronized ,I am not sure about that.
sunil Choudhary

----------------------------------<br />"Learning is weightless, a treasure you can always carry easily."<br />-Chinese Proverb
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
All methods in Math are static because you can't construct an instance of Math, the constructors are private, so you can't use them.
As for synchronization, when I looked up the API I got this:
public static double random()
so, random() isn't synchronized.
Also, catchbollu, does not conform to our naming policy here at the Ranch. We appreicate your posts and questions and we want you to continue, but can you please re-register with a more appropriate name.
Thanks,
Bill
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
Bill,
It *is* synchronized.
<pre>
=============
public static synchronized double random() {
if (randomNumberGenerator == null)
randomNumberGenerator = new Random();
return randomNumberGenerator.nextDouble();
}
=============
</pre>
A method being synchronized or not is considered to be an implementation detail, and thus is not part of the contract for that method. So, it is prudent that such info is not included in the API.
Now, as per the reason it is marked as synchronized. Well, it sets private static variable randomNumberGenerator of Math. You want to have synchronized access to such code to avoid having concurrently running threads one overwrite the acts of the other one.
Panagiotis.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: random( )
 
Similar Threads
Static
URLyBIRD lock question
Implementing Probability in Java ?
Are these two methods equivalent?
Is Random Thread Safe ?