Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8874963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:51:20+00:00 2026-06-14T18:51:20+00:00

I need to generate random integers within a maximum. Since performance is critical ,

  • 0

I need to generate random integers within a maximum. Since performance is critical, I decided to use a XORShift generator instead of Java’s Random class.

long seed = System.nanoTime();
seed ^= (seed << 21);
seed ^= (seed >>> 35);
seed ^= (seed << 4);

This implementation (source) gives me a long integer, but what I really want is an integer between 0 and a maximum.

public int random(int max){ /*...*/}

What it is the most efficient way to implement this method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:51:23+00:00Added an answer on June 14, 2026 at 6:51 pm

    I had some fun with your code and came up with this:

    public class XORShiftRandom {
    
    private long last;
    private long inc;
    
    public XORShiftRandom() {
        this(System.currentTimeMillis());
    }
    
    public XORShiftRandom(long seed) {
        this.last = seed | 1;
        inc = seed;
    }
    
    public int nextInt(int max) {
        last ^= (last << 21);
        last ^= (last >>> 35);
        last ^= (last << 4);
        inc += 123456789123456789L;
        int out = (int) ((last+inc) % max);     
        return (out < 0) ? -out : out;
    }
    
    }
    

    I did a simple test and it is about Four times as fast as the java.util.Random

    If you are intrested in how it works you can read this paper:

    Disclamer:

    The code above is designed to be used for research only, and not as a
    replacement to the stock Random or SecureRandom.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function to generate random integers. (assume Java long type for now,
I need to generate 8 random integers, but they need to be unique, aka
I need to generate a (pseudo) random sequence of N bit integers, where successive
I need to generate pseudo-random codes for people to use to access my site.
I need to generate arbitrarily large random integers in the range 0 (inclusive) to
I need to generate an infinite stream of random integers, with numbers to be
Like my question, i need to generate random numbers that have identical pairs between
I need to generate a random port number between 2000-65000 from a shell script.
I need to generate an random array from a bigger array of int without
A specific example I need to generate a random number between 0 and 2,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.