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

 From: Cosmin M. Sent on: Friday, January 25, 2013 5:25 PM
Well no solution presented yet is "ideal" although some are decent. I think http://grepcode.com/file/repo1.maven.org/maven2/org.apache.commons/commons-math3/3.0/org/apache/commons/math3/random/RandomDataImpl.java#RandomDataImpl.nextLong%28long%2Clong%29 gets close enough, but is not perfect either for many reasons.
I would love to investigate this further, but is definitely not an easy one to solve.

On Fri, Jan 25, 2013 at 5:20 PM, Emanuele Tagliaferri <[address removed]> wrote:
yeah, before i was wrong, this is really right!

On 01/25/[masked]:04 PM, Cosmin Marginean wrote:
Right...

private static long generateRandom(long min, long max) {
double rnd = Math.random();
if (min < 0 && max > 0) {
return min + (long) (Math.abs(min - (min/Long.MIN_VALUE)) * rnd) + (long) (max * rnd);
} else {
return min + (long) (Math.abs(max + 1 - min) * Math.random());
}
}

On Fri, Jan 25, 2013 at 4:31 PM, Wesley Hall <[address removed]> wrote:
Oh sorry...

Sivaji,

Nice! Actually I started writing this because I wasn't able to find a
solution out there in the open source world. I will have a look at the
code for the commons math stuff tonight to see how they approached it.

On Fri, Jan 25, 2013 at 4:13 PM, Wesley Hall <[address removed]> wrote:
> Wow, loving the volume of responses :).
>
> Just one request, it would be helpful to me if people can keep the
> subject line consistent, this keeps gmail thread organisation working
> and is helpful for my sanity.
>
> Some comments for various people:
>
> Sam,
>
> I am not too worried about efficiency or the distribution of
> java.util.Random itself. This is not going to be used in highly
> contended code. The solution you provide suffers from some problems,
> test it with values like Long.MIN_VALUE and Long.MAX_VALUE as the
> parameters and -1 to MAX etc. You will see some strange behaviour.
>
> Cosmin,
>
> I looked at your solution and it looked like it was just mathematical
> enough to work, alas... MIN_VALUE and MAX_VALUE provides the same
> result on every call, so something is certainly wrong ;).
>
> Jahan,
>
> Try the above types of parameters as well, you may well find that you
> have a problem ;).
>
> Stuart,
>
> You might win actually :). The stuff after the BigInteger stuff
> (which, *spoiler* is pretty much what I did), looks like it should
> have distribution problems. Especially with the way that you do that
> 'correct' thing and push everything between the bounds. That just
> looks to me like you will end up with a disproportionate numbers at
> the boundaries but I did a very quick distribution test and this
> didn't seem to be happening. I will look a little more tonight. My
> solution is a bit different but yours might well be better.... watch
> this space :).
>
> Wes
>
> On Fri, Jan 25, 2013 at 10:56 AM, Wesley Hall <[address removed]> wrote:
>> 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]

--
Cosmin Marginean

--
This message was sent by Cosmin Marginean ([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]

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

--
Cosmin Marginean

### 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

Discounts on ebooks and O'Reilly conferences

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

• ##### Young Professionals in London

26,453 Young Professionals

• ##### London μServices (Microservices)­ User Group

2,790 μServicers

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

11,103 Technopreneurs

4,501 Clouds

4,074 People

• ##### London Software Craftsmanship Community

4,224 Software Crafters