Random function not working

Bug #516195 reported by Eric Lambert on 2010-02-02
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Gearman Java
Eric Lambert

Bug Description

This bug was originally filed by Norberto Leite in the gearman-ij project. Refilling it here ....

Hi there,

The GearmanClientImpl class has a bug on method getSessionForTask() when trying to collect a random value for the list of possible gearmanSession on the list. This line :
int s = (int) Math.round((Math.random() * (sessions.size() - 1)));
I have been testing this and it provides not "very random" at all.(most common value is 0!).

I recommend the use of :
Random rand = new Random(System.currentTimeMillis());
int s = rand.nextInt(sessions.size());

There's a relative gain in performance and more decoupled organization of code without the need to use static functions:
(100000000 iterations loop)
Random -> 2287 mls

Math.random -> 6598 mls

Random with Constructor time -> 2320 mls


Eric Lambert (elambert) on 2010-04-12
Changed in gearman-java:
status: New → Fix Committed
Eric Lambert (elambert) on 2010-09-10
Changed in gearman-java:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers