# Re: [ljc] A little Friday morning challenge for the group...

 From: Dominic F. Sent on: Friday, January 25, 2013 4:04 PM
Ignore my email. It came out long after I intended it to (approval delays :-)) and without testing it for the max and min values. Rookie mistake :-) Will take a crack at it until Sunday and get back.

Cheers
Dominic

On Fri, Jan 25, 2013 at 3:42 PM, Dominic Fernandes <[address removed]> wrote:
Hi Wesley,

Dan's solution will work with the right use of parentheses  as follows -

public static long generateRandom(long min, long max)
{
return (long)((max - min + 1) * Math.random()) + min;
}

try it out and let me know.

Cheers
Dominic

On Fri, Jan 25, 2013 at 11:43 AM, Wesley Hall <[address removed]> wrote:
Dan,

public static long generateRandom(long min, long max) {

return (long)((max+1-min) * Math.random() + min);

}

public static void main(String[] args) {

System.out.println(generateRandom(-1, Long.MAX_VALUE));

}
}

When I run it I get values like...

This, looks to me, to be less than the min boundary ;), so your solution doesn't work i'm afraid.

On Fri, Jan 25, 2013 at 11:20 AM, Dan Haywood <[address removed]> wrote:
return (max+1-min) * Math.random() + min

Hello folks,

I came across what turned out to be a pretty interesting problem on a
project of mine last night, and I thought I would share it with you
guys. I am interested to see if anyone can come up with a better
solution than I did (highly likely I suspect :)).

Your mission, should you choose to accept it, is to write a method, in
Java, with the following signature...

public static long generateRandom(long min, long max) {
// code here
}

Such that the method generates a random long value that is between the
two parameters provided (inclusive). The only restriction on the
parameters is that max >= min, for brevity you can assume this is
ensured outside of the method and that you don't need to check inside.

The first person to provide a working solution gets to look like a
hero... and if you provide a solution that I think is better than the
one I have, then I will (with your permission), use this version in
the library that I am about to open source and give you contributor
credit.

GO....

Wes

--
http://www.meetup.com/Londonjavacommunity/
This message was sent by Wesley Hall ([address removed]) from LJC - London Java Community.
Set my mailing list to email me

As they are sent
http://www.meetup.com/Londonjavacommunity/list_prefs/?pref=1

In one daily email
http://www.meetup.com/Londonjavacommunity/list_prefs/?pref=2

Don't send me mailing list messages
http://www.meetup.com/Londonjavacommunity/list_prefs/?pref=0
Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]

--
This message was sent by Dan Haywood ([address removed]) from LJC - London Java Community.

--
This message was sent by Wesley Hall ([address removed]) from LJC - London Java Community.

--
This message was sent by Dominic Fernandes ([address removed]) from LJC - London Java Community.

### London, United Kingdom

Founded Nov 26, 2007

#### People in this Meetup are also in:

• ##### Technopreneurs - Tech Startups and Entrepreneurs

11,486 Technopreneurs

• ##### London Python

4,622 Pythonistas

• ##### Virtual Java User Group

15,306 vJUGers

• ##### London DevOps

5,538 DevOps Doves

• ##### The London Android Group - Londroid

4,569 Android Explorers

• ##### All Hands on DevOps - London

1,607 DevOpers