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

 From: Neil B. Sent on: Friday, January 25, 2013 11:57 AM

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.

Unfortunately you cannot use a long to enumerate all possible values of long, as it is a signed type. Dan's solution will work for values of min and max such that max - min < Long.MAX_VALUE/2. If you need a larger range then you'll have to resort to BigInteger, I suspect.

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.
Set my mailing list to email me As they are sent | In one daily email | Don't send me mailing list messages

Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]

### London, United Kingdom

Founded Nov 26, 2007

#### Organizers:

• ##### Our Blog

Read the latest news from the LJC

• ##### RecWorks Ltd

Fixing Tech Recruitment using the Power of Community

• ##### jClarity

Java/JVM Performance Analysis Tools & mentoring for Java related matters

• ##### LJC Aggrity

Our LJC Aggrity site contains blog posts from our members

• ##### LJC Book Club

Our Book club with book reviews from our members

• ##### Devoxx UK

Java Community Conference in collaboration with the LJC, 8-10th June 16

• ##### SkillsMatter

"Host, help organise, promote, film many of our meetings."

• ##### IBM

Build Enterprise-grade apps at start-up speed.

• ##### New Relic

New Relic makes sense of billions of metrics a day in real time.

• ##### Hazelcast

Hazelcast is the leader in operating in-memory computing.

• ##### Java.Net

We are an official Java User Group recognised by Oracle's JUG program

• ##### O'Reilly

40% discount on printed books and 50% on e-books.

• ##### Craft Rebellion

Your choice of fresh craft beer, delivered. For 10% off use ‘LJC'

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

• ##### London Ajax User Group

1,475 Software Engineers

• ##### Docker London

2,910 Dockers

• ##### Meteor London

1,909 Meteorites

• ##### AppsJunction-De­velopers, Startups, Investors, Speakers

3,842 Apps Enthusiasts

• ##### Agile Testing

2,936 Test Enthusiasts

844 Members