align-toparrow-leftarrow-rightbackbellblockcalendarcamerachatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-crosscrosseditemptyheartfacebookfullheartglobegoogleimagesinstagramlocation-pinmagnifying-glassmailmoremuplabelShape 3 + Rectangle 1outlookpersonplusImported LayersImported LayersImported Layersshieldstartwitteryahoo

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

From: Tom H.
Sent on: Friday, January 25, 2013 6:37 PM
n 25/01/2013, Wesley Hall <[address removed]> wrote:
> public static long generateRandom(long min, long max) {
>    // code here
> }

Off the top of my head (don't have a Java compiler on my machine).

    // Doing this properly, right?
    private static final ThreadLocal<Rando­m> rng = new ThreadLocal<Rando­m>() {
        @Override protected Random initialValue() {
            return new SecureRandom();

    public static long generateRandom(long min, long max) {
        // ...special case handling here...
        final Random g = rng.get();
        long value;
        do {
            value = g.nextLong();
        } while (!(min <= value && value <= max));
        return value;

You may want to optimise for small ranges (copying code from
nextInt(int)) as it may take some time to complete.


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