5b43e682db733a3140aceafa4f474afb

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

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:

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!
public static long randomNumber(int min, int max) {
	int number = new Random().nextInt(++max);		
	return number < min ? number += min : number;
}
D41d8cd98f00b204e9800998ecf8427e

private int

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

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

public

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

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

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)

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.

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.

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

C94042dc5f08a4594504d689580a9e2b

Locksmith in Haddon Township

September 13, 2011, September 13, 2011 22:50, permalink

No rating. Login to rate!

Well done. We are astounded with the standard of the knowledge provided. I expect that you keep up with the brilliant job achieved.

C94042dc5f08a4594504d689580a9e2b

Locksmith in Haddon Township

September 13, 2011, September 13, 2011 22:50, permalink

No rating. Login to rate!

Well done. We are astounded with the standard of the knowledge provided. I expect that you keep up with the brilliant job achieved.

E230a9dd061708910438c085ae127145

massage London

December 24, 2011, December 24, 2011 01:46, permalink

No rating. Login to rate!

Very very nice blog!
The Massage Rooms provides massage london, london masage, massages, massage services london in all over UK.

E230a9dd061708910438c085ae127145

massage London

December 24, 2011, December 24, 2011 01:46, permalink

No rating. Login to rate!

Very very nice blog!
The Massage Rooms provides massage london, london masage, massages, massage services london in all over UK.

E230a9dd061708910438c085ae127145

massage London

December 24, 2011, December 24, 2011 01:46, permalink

No rating. Login to rate!

Very very nice blog!
The Massage Rooms provides massage london, london masage, massages, massage services london in all over UK.

E230a9dd061708910438c085ae127145

massage London

December 24, 2011, December 24, 2011 01:47, permalink

No rating. Login to rate!

Very very nice blog!
The Massage Rooms provides massage london, london masage, massages, massage services london in all over UK.

779a4410c342e77797a0714fb0148368

packers and movers lucknow

December 8, 2011, December 08, 2011 10:21, permalink

No rating. Login to rate!

Anil Packers Movers is a Leading Packers and Movers, Movers and packers delhi, movers and packers gurgaon, mover and packers mumbai, movers and packers kolkata, movers and packers bangalore, movers and packers pune, movers and packers noida, packers and movers patna, packers and movers ranchi, packers and movers bhubaneshwar, packers and movers lucknow, packers and movers ranchi. call me 09430890559

779a4410c342e77797a0714fb0148368

packers and movers lucknow

December 8, 2011, December 08, 2011 10:21, permalink

No rating. Login to rate!

Anil Packers Movers is a Leading Packers and Movers, Movers and packers delhi, movers and packers gurgaon, mover and packers mumbai, movers and packers kolkata, movers and packers bangalore, movers and packers pune, movers and packers noida, packers and movers patna, packers and movers ranchi, packers and movers bhubaneshwar, packers and movers lucknow, packers and movers ranchi. call me 09430890559

779a4410c342e77797a0714fb0148368

packers and movers lucknow

December 8, 2011, December 08, 2011 10:21, permalink

No rating. Login to rate!

Anil Packers Movers is a Leading Packers and Movers, Movers and packers delhi, movers and packers gurgaon, mover and packers mumbai, movers and packers kolkata, movers and packers bangalore, movers and packers pune, movers and packers noida, packers and movers patna, packers and movers ranchi, packers and movers bhubaneshwar, packers and movers lucknow, packers and movers ranchi. call me 09430890559

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

minibus hire Essex

December 28, 2011, December 28, 2011 03:44, permalink

No rating. Login to rate!

This is a very good article on a very good topic.
Wish you a very happy new year.

E230a9dd061708910438c085ae127145

minibus hire Essex

December 28, 2011, December 28, 2011 03:44, permalink

No rating. Login to rate!

This is a very good article on a very good topic.
Wish you a very happy new year.

E230a9dd061708910438c085ae127145

canon photocopier

December 28, 2011, December 28, 2011 03:45, permalink

No rating. Login to rate!

This is a very good article on a very good topic.
Wish you a very happy new year.

E230a9dd061708910438c085ae127145

mot in east London

December 28, 2011, December 28, 2011 03:46, permalink

No rating. Login to rate!

This is a very good article on a very good topic.
Wish you a very happy new year.

E230a9dd061708910438c085ae127145

massage london

December 28, 2011, December 28, 2011 02:43, permalink

No rating. Login to rate!

this is a very nice blog post. It is quite good for me and very helpful. Thanks

E230a9dd061708910438c085ae127145

London massage

December 28, 2011, December 28, 2011 02:44, permalink

No rating. Login to rate!

this is a very nice blog post. It is quite good for me and very helpful. Thanks
Wish you a very happy new year.

E230a9dd061708910438c085ae127145

dry cleaners London

December 28, 2011, December 28, 2011 02:45, permalink

No rating. Login to rate!

It is quite good for me and very helpful. Thanks
Wish you a very happy new year.

E230a9dd061708910438c085ae127145

dry cleaners Chelmford

December 28, 2011, December 28, 2011 02:47, permalink

No rating. Login to rate!

It is quite good for me and very helpful. Thanks

55502f40dc8b7c769880b10874abc9d0

premiumjetset.blogspot.com

January 8, 2012, January 08, 2012 03:30, permalink

No rating. Login to rate!

great article thanks for sharing

Your refactoring





Format Copy from initial code

or Cancel