5b43e682db733a3140aceafa4f474afb

The title sais it: you'll get a random number within a given min and max.

1
2
3
public static long randomNumber(int min, int max) {
	return Math.round((Math.random() * (max - min)) + min);
}

Refactorings

No refactoring yet !

A2c8fecfd1fb707dd0a8f292ade77e1e

typefreak

October 1, 2007, October 01, 2007 05:35, permalink

1 rating. Login to rate!

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));
}
Fa5f7147169e473604623afc070e6281

Carlos Daniel de Mattos Mercer

October 1, 2007, October 01, 2007 10:55, permalink

1 rating. Login to rate!
1
2
3
4
public static long randomNumber(int min, int max) {
	int number = new Random().nextInt(++max);		
	return number < min ? number += min : number;
}
Avatar

private int

October 1, 2007, October 01, 2007 10:59, permalink

1 rating. Login to rate!
1
2
3
4
private static int randomNumber(int min, int max) {
	int number = new Random().nextInt(++max);		
	return number < min ? number += min : number;
}
Avatar

public

October 1, 2007, October 01, 2007 11:22, permalink

1 rating. Login to rate!
1
2
3
4
public static int randomNumber(int min, int max) {
	int number = new Random().nextInt(++max);		
	return number < min ? number += min : number;
}
Avatar

Cameron Hatfield

October 1, 2007, October 01, 2007 11:59, permalink

1 rating. Login to rate!

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;
}
0848508770c8819426c4e8bcb5ebda58

Gordon Mohr

October 1, 2007, October 01, 2007 16:00, permalink

2 ratings. Login to rate!

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);
}
5b43e682db733a3140aceafa4f474afb

diskostu

October 2, 2007, October 02, 2007 01:01, permalink

No rating. Login to rate!

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.

0848508770c8819426c4e8bcb5ebda58

Gordon Mohr

October 2, 2007, October 02, 2007 13:34, permalink

No rating. Login to rate!

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.

336d5ebc5436534e61d16e63ddfca327

Ben

January 18, 2009, January 18, 2009 00:53, permalink

No rating. Login to rate!

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)));
}
3aa63bb586c5c59f9fc6b2afb9417821

software development london

August 18, 2009, August 18, 2009 08:48, permalink

No rating. Login to rate!

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

5d2dab841e4379fda992ef07a7ac4308

Canvas Prints

March 2, 2010, March 02, 2010 15:41, permalink

No rating. Login to rate!

Yes I thought that it would, thanks for this, good post

Your refactoring





Format Copy from initial code

or Cancel