1 2 3
public static long randomNumber(int min, int max) { return Math.round((Math.random() * (max - min)) + min); }
Refactorings
No refactoring yet !
typefreak
October 1, 2007, October 01, 2007 05:35, permalink
Why do you return a 'long', if the range is only specified by ints? The result always fits in an integer.
This is more or less the standard method for generating a random number. The only (usefull) change I can think of is:
1 2 3
public static int randomNumber(int min, int max) { return min + (int)(Math.random() * (max - min)); }
Carlos Daniel de Mattos Mercer
October 1, 2007, October 01, 2007 10:55, permalink
Try to use MTRandom ("A very fast random number generator"):
http://www.koders.com/java/fid0498E2527646FD994A3DC7288DC13144CC0EA8FF.aspx
http://goui.net/doc/net/goui/util/MTRandom.html
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
1 2 3 4
public static long randomNumber(int min, int max) { int number = new Random().nextInt(++max); return number < min ? number += min : number; }
private int
October 1, 2007, October 01, 2007 10:59, permalink
1 2 3 4
private static int randomNumber(int min, int max) { int number = new Random().nextInt(++max); return number < min ? number += min : number; }
public
October 1, 2007, October 01, 2007 11:22, permalink
1 2 3 4
public static int randomNumber(int min, int max) { int number = new Random().nextInt(++max); return number < min ? number += min : number; }
Cameron Hatfield
October 1, 2007, October 01, 2007 11:59, permalink
This method will fail when the difference between min and max is smaller then a random number that is generated that is less then min.
(Min = 20, Max = 21, Number = 5)
1 2 3 4 5
public static int randomNumber(int min, int max) { int number = new Random().nextInt(++max); return number < min ? number += min : number; }
Gordon Mohr
October 1, 2007, October 01, 2007 16:00, permalink
The original implementation is awful. Passed min 0 and max 2, it has about a 25% chance of returning 0, 50% chance of returning 1, and 25% chance of returning 2.
Passing through a double or doing rounding only introduces potential for blatant or subtle nonuniformity. Also, it would be wise to follow the underlying Java API's convention of being inclusive of the min value, exclusive of the max value.
The following does the right things.
If this were a performance critical operation you might cache the Random instance in a static field. If you wanted reproducible 'random' series (as in simulations) you would cache a Random instance that was created with a client-contributed seed value. If you needed very high-quality (pseudo)randomness you would use SecureRandom instead, perhaps with a non-default algorithm/provider package.
1 2 3
public static int randomNumber(int min, int max) { return min + (new Random()).nextInt(max-min); }
diskostu
October 2, 2007, October 02, 2007 01:01, permalink
Thanks for your infos Gordon - I'm not a very experienced Java programmer, so your "awful" is OK for me. Haven't tried my implementation with min=0 and max=2, so... thanks for your comment.
Gordon Mohr
October 2, 2007, October 02, 2007 13:34, permalink
You're welcome, thanks for your gracious response to my strong language. (Didn't mean to be harsh, just wanted to make a strong point.)
FYI, the nonuniformity of the original code means the two endpoints always have half the chance of any internal number -- because the scaled-up double has to be within the first or last 0.5 range to be rounded to the endpoints, but can be within a 1.0 range to be rounded to an internal number. For example, with min=0 and max=3, 0 has a 1/6 chance, 1 and 2 a 1/3 chance each, and 3 a 1/6 chance.
Ben
January 18, 2009, January 18, 2009 00:53, permalink
What Gordon posted will fail with for example inputs of 0 and 1. Mine wont.
1 2 3 4
public static int myRandom (int min, int max) { return min + (int)Math.round((Math.random() * (max - min))); }
software development london
August 18, 2009, August 18, 2009 08:48, permalink
Interesting,
I'm much easier with return min + (int)Math.round((Math.random() * (max - min)));
it is more easy to understand
Thanks for bringing this up
Canvas Prints
March 2, 2010, March 02, 2010 15:41, permalink
Yes I thought that it would, thanks for this, good post
The title sais it: you'll get a random number within a given min and max.