addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwchatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrosseditemptyheartexportfacebookfolderfullheartglobegmailgoogleimagesinstagramlinklocation-pinmagnifying-glassmailminusmoremuplabelShape 3 + Rectangle 1outlookpersonplusprice-ribbonImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruseryahoo

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

From: Somay
Sent on: Monday, January 28, 2013 3:49 PM
If high performance is a requirement for your application and you want to avoid creating new BigInteger objects in the heap (and garbage collecting them), you can look at solving this using primitives only.

This solution uses some algebra, java, and memory knowledge. It address both issues:

1. Overflow: by comparing min and max values relative to the biggest possible long value (Long. MAX_VALUE).
2.Distribution: longs in java are represented in 64 bits in the memory while the mantissa of the double in java is represented in the memory in 52 bits. Which means the Math.random() method will not return enough values to cover all possible rangers of the long values. To resolve this, divide the long range values into sections (not bigger from the double mantissa size) selected a section at random, then select a random value in the section.

Here is the code:

	public static long generateRandom(long min, long max) {
		long o = Long.MAX_VALUE / Long.MAX_VALUE - Long.MIN_VALUE / Long.MAX_VALUE;
		long m = (long) Math.pow(2, 52);
		
		long rand;
		long d = max/o - min/o;
		
		if (d < m ) {
			rand = min + (long)(Math.random()­ * (max - min));
		} else {
			long s = (d / m) * o;
			long r =(long)(Math.random(­) * s);
			min += r * m; 
			max = min + m ;
			
			rand = generateRandom(min, max);
		}
		
		return rand;
	} 

Somay

-----Original Message-----
From: [address removed] [mailto:[address removed]] On Behalf Of Wesley Hall
Sent: 25 January[masked]:01
To: [address removed]
Subject: [ljc] A little Friday morning challenge for the group...

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




--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed]) http://www.meetup...­
This message was sent by Wesley Hall ([address removed]) from LJC - London Java Community.
To learn more about Wesley Hall, visit his/her member profile: http://www.meetup...­
Set my mailing list to email me

As they are sent
http://www.meetup...­

In one daily email
http://www.meetup...­

Don't send me mailing list messages
http://www.meetup...­
Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]

Our Sponsors

  • 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

  • JRebel

    Free 3 month J-Rebel license.

  • 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:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy