Re: Random Int between x and y
Re: Random Int between x and y
- Subject: Re: Random Int between x and y
- From: Kieran Kelleher <email@hidden>
- Date: Wed, 10 Mar 2004 11:04:12 -0500
Correct me if I'm wrong, but I think casting using (int) will skew the
results due to rounding down truncation, so better to use Math.round
instead of (int) cast. The probability of having the result be equal to
m2 is greatly reduced using the (int) cast.
It just occurred to me, probably m1 and m2 should be converted to
doubles and make m1 = m1 - 0.49999 and make m2 = m2 + 0.49999 to get
true probability across the range of numbers since each possible number
has a +/- 0.5 spread each side of the final integer selection.
-Kieran
On Mar 10, 2004, at 9:47 AM, Michael Engelhart wrote:
To be more specific:
public class Test {
public static void main(String args[]) {
int m1 = 10;
int m2 = 25;
int rn = (int) ((Math.random() * (m2-m1)) + m1);
System.out.println(rn);
}
}
Mike
On Mar 10, 2004, at 9:33 AM, email@hidden wrote:
Hi David
On 10 Mar 2004, at 13:46, David Griffith wrote:
Hi all,
Is there an easy way to generate a random int in the range x <= int
<= y?
I have found a couple of methods but they don't appear to do what I
need.
Use java.lang.Math.random().
If you need a cryptographically strong number, you can look at
java.security.SecureRandom
Cheers,
-- Denis.
I'm sure there's a simple answer....
Regards,
Dave.
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.