Re: Random Int between x and y
Re: Random Int between x and y
- Subject: Re: Random Int between x and y
- From: David Griffith <email@hidden>
- Date: Wed, 10 Mar 2004 17:43:21 +0100
Thanks to everyone for that - I had seen the functions but didn't know how
to use them for a specific range. This has helped a lot.
Kieran,
I am Irish of course, but living in the Costa Del Sol in southern Spain.
Moved here 2 years ago. I haven't been looking for WO business but have
been approached with a large project and am having great fun with WO! I'll
post a link when it's done as I think the products they are selling could
benefit many WO developers - I am using them now and they really work.
(These are not tools I am talking about, they are natural stress reducing
vitamin/mineral complexes).
Best of luck!
Dave.
> 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.
_______________________________________________
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.