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 9212255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:28:51+00:00 2026-06-18T01:28:51+00:00

When using class java.util.Random, how can one get the value obtained from calling the

  • 0

When using class java.util.Random, how can one get the value obtained from calling the method nextInt() N times, but in a much more efficient way (in O(1) specifically)?

For example, if I construct a Random object with a particular seed value, and I want to get the 100,000th “nextInt() value” (that is, the value obtained after calling the method nextInt() 100,000 times) in a fast way, could I do it?

Assume, for simplicity, version 1.7.06 of the JDK, since it may be required to know the exact values of some private fields in class Random. And speaking of, I found the following fields to be relevant in the calculation of a random value:

private static final long multiplier = 0x5DEECE66DL;
private static final long addend = 0xBL;
private static final long mask = (1L << 48) - 1;

After exploring a bit about randomness, I found that random values are obtained using a Linear congruential generator. The actual method that executes the algorithm is method next(int):

protected int next(int bits) {
    long oldseed, nextseed;
    AtomicLong seed = this.seed;
    do {
        oldseed = seed.get();
        nextseed = (oldseed * multiplier + addend) & mask;
    } while (!seed.compareAndSet(oldseed, nextseed));
    return (int)(nextseed >>> (48 - bits));
}

The relevant line for the algorithm is the one that obtains the next seed value:

nextseed = (oldseed * multiplier + addend) & mask;

So, to be more specific, is there a way that I can generalize this formula to obtain the “nth nextseed” value? I’m assuming here that after having that, I can then simply obtain the nth int value by letting the variable “bits” be 32 (the method nextInt() simply calls next(32) and returns the result).

Thanks in advance

PS: Perhaps this is a question more suitable for mathexchange?

  • 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-18T01:28:52+00:00Added an answer on June 18, 2026 at 1:28 am

    You can do it in O(log N) time. Starting with s(0), if we ignore the modulus (248) for the moment, we can see (using m and a as shorthand for multiplier and addend) that

    s(1) = s(0) * m + a
    s(2) = s(1) * m + a = s(0) * m² + (m + 1) * a
    s(3) = s(2) * m + a = s(0) * m³ + (m² + m + 1) * a
    ...
    s(N) = s(0) * m^N + (m^(N-1) + ... + m + 1) * a
    

    Now, m^N (mod 2^48) can easily be computed in O(log N) steps by modular exponentiation by repeated squaring.

    The other part is a bit more complicated. Ignoring the modulus again for the moment, the geometric sum is

    (m^N - 1) / (m - 1)
    

    What makes computing this modulo 2^48 a bit nontrivial is that m - 1 is not coprime to the modulus. However, since

    m = 0x5DEECE66DL
    

    the greatest common divisor of m-1 and the modulus is 4, and (m-1)/4 has a modular inverse inv modulo 2^48. Let

    c = (m^N - 1) (mod 4*2^48)
    

    Then

    (c / 4) * inv ≡ (m^N - 1) / (m - 1) (mod 2^48)
    

    So

    • compute M ≡ m^N (mod 2^50)
    • compute inv

    to obtain

    s(N) ≡ s(0)*M + ((M - 1)/4)*inv*a (mod 2^48)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using java.util.Timer class and I am using its schedule method to perform
I'm using java 6 random (java.util.Random,linux 64) to randomly decide between serving one version
I am creating a class that implements java.util.List . My class is using E
When using java.util.logging, the log level for a specific class (i.e. for a child
I get the Yahoo IP address using InetAddress class in java. The result of
I'm experimenting Java Multi-Threading using synchronization on method comparing with Atomic variables (java.util.concurrent.atomic package).
If you had a single class you needed to import, say java.util.Random, would naming
How I am going to display random of integers using this code? import java.util.*;
I can't get graphics/text to draw on my panel/canvas/window in my Java program (using
Using the java.util.concurrent.locks.ReentrantLock library as follows: Two threads generate a random number and use

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.