Random function not working

Bug #516194 reported by Eric Lambert
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Gearman Java
Invalid
Medium
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

Cheers,
Norberto

Revision history for this message
Eric Lambert (elambert) wrote :

this is a duplicate of #516194

Changed in gearman-java:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.