Dr Heinz M. Kabutz, editor of Java Specialist's Newsletter, just published another fascinating article, this time about Java's Math.random(). This time with help from Dr. Wolfgang Laun, from Vienna. You can read the whole post here:
http://www.javaspecialists.eu/archive/Issue198.html
The important information is that, to get a random number from zero to some_int, we shouldn't use this code:
(int)(Math.random() * some_int)
Instead we should use nextInt(some_int) from java.util.Random class. Since Java 7, due to concurrency, we should use:
ThreadLocalRandom.current().nextInt(some_int);
But things are not so easy. There is a bug in Java 7 versions prior to 1.7.0_02, that results in returning the same value for all threads. I strongly recommend to any professional to read the whole post and also older posts from Dr. Kabutz's newsletter.
BTW: Dr Heinz M. Kabutz is a member of Java Champion group, an exclusive group of passionate Java technology and community leaders who are community-nominated and selected under a project sponsored by Oracle.
Tuesday, February 14, 2012
Subscribe to:
Comments (Atom)