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

  • SEARCH
  • Home
  • 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 8807589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:24:17+00:00 2026-06-14T02:24:17+00:00

Okay, so I’m doing some research on how random numbers are generated with the

  • 0

Okay, so I’m doing some research on how random numbers are generated with the Math.random method. So far I learned that it starts with a “random” seed, and that seed is plugged into some complex equation to create a random number. If the seed is always the same, will the outcome always be the same?

I heard that the seeds for Math.random are generated through the current time, is that correct? They must use the current time all the way down to the mili-seconds or something, because if you didn’t you would get the same outcome.

What exactly is the seed? Is it the time such as “10:45” or the time AND date such as “10:45 11/8/12” or some combination?

How can I find the seed, so I can predict the output?

I want to be able to plug this:

alert(Math.floor((Math.random()*10)+1));

into my url bar, and be able to predict the result. Is that possible?

  • 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-14T02:24:18+00:00Added an answer on June 14, 2026 at 2:24 am

    I looked through the Rhino source code to find out which pseudo-random function they use. Apparently they fall back to the Math.random function defined in the Java standard library.

    The documentation for Math.random says:

    Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

    When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression

    new java.util.Random
    

    This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else.

    This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.

    So I checked the documentation for java.util.Random and found this (for the default constructor):

    Creates a new random number generator. Its seed is initialized to a value based on the current time:

    public Random() { this(System.currentTimeMillis()); }
    

    Two Random objects created within the same millisecond will have the same sequence of random numbers.

    So now we know for sure that the seed is the current time in milliseconds. Also, the documentation for the second constructor says:

    Creates a new random number generator using a single long seed:

    public Random(long seed) { setSeed(seed); }
    

    Used by method next to hold the state of the pseudorandom number generator.

    The documentation for the setSeed method says:

    Sets the seed of this random number generator using a single long seed. The general contract of setSeed is that it alters the state of this random number generator object so as to be in exactly the same state as if it had just been created with the argument seed as a seed. The method setSeed is implemented by class Random as follows:

    synchronized public void setSeed(long seed) {
        this.seed = (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1);
        haveNextNextGaussian = false;
    }
    

    The implementation of setSeed by class Random happens to use only 48 bits of the given seed. In general, however, an overriding method may use all 64 bits of the long argument as a seed value. Note: Although the seed value is an AtomicLong, this method must still be synchronized to ensure correct semantics of haveNextNextGaussian.

    The actual method used to generate the random number is nextDouble:

    Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator’s sequence.

    The implementation of the nextDouble function is as follows:

    public double nextDouble() {
        return (((long)next(26) << 27) + next(27))
            / (double)(1L << 53);
    }
    

    Clearly it depends on the next function:

    Generates the next pseudorandom number. Subclass should override this, as this is used by all other methods.

    The implementation of the next function is as follows:

    synchronized protected int next(int bits) {
        seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
        return (int)(seed >>> (48 - bits));
    }
    

    That’s the pseudo-random function you are looking for. As it’s said in the documentation:

    This is a linear congruential pseudorandom number generator, as defined by D. H. Lehmer and described by Donald E. Knuth in The Art of Computer Programming, Volume 2: Seminumerical Algorithms, section 3.2.1.

    Note however that this is only the random number generator used by Rhino. Other implementations like Spidermonkey and V8 may have their own pseudo-random number generators.

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

Sidebar

Related Questions

Okay, so this is the line that's returning null. What am I doing wrong
Okay so I am having some issues when doing some NSXMLParsing. The parsers are
Okay, next PHPExcel question. I have an HTML form that users fill out and
Okay - I have a dilemma. So far my script converts page titles into
okay, i'm setting up a multi-user chat system. i have a messages table, that
Okay I am having some problems with being able to change bitmaps when a
Okay, I really know this has GOT to be the long way around doing
Okay I do apologise that I'm out of my depth here. I'm not the
Okay, I know you can override the to_xml method for a single instance of
Okay So this doesnt make sense to me.... maybe someone can shed some light.

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.