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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:49:34+00:00 2026-06-12T21:49:34+00:00

Possible Duplicate: Java random always returns the same number when I set the seed?

  • 0

Possible Duplicate:
Java random always returns the same number when I set the seed?

I’m using the Java’s Random class and the nextInt() method to get random numbers. But it seems like the numbers are always in the same order. Is there a way to fix this? I know some random generators take in a seed value, then you use the system timer for the seed.

Code:

  • 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-12T21:49:35+00:00Added an answer on June 12, 2026 at 9:49 pm

    This is what you really need to refer to https://stackoverflow.com/a/11705615/1143977. Quoting my answer from the reference link.

    There are two issues causing what you see. The first is that the code sets a seed value for a Random instance. The second is that the instance method “random” instances a new Random object and then immediately sets its seed with the same seed every time. The combination of these two guarantees that, for the same value of i, the method “random” will always return the same value and it will always be the first in the sequence that the seed always generates.

    Assuming setting the seed is mandatory, to get the next value in the sequence instead of the same first value of the sequence every time, the randnum instance of Random can’t have its seed set every time just before its next method gets called. To fix that, move the randnum local variable instance of Random from the scope of the random instance method to the class scope. Second, set the seed only when random is assigned a Random instance or only to get same sequence of results from it to start over again. Class Random’s setSeed(long seed) instance method can’t execute in the class scope, so the constructor has to set it using the Random constructor with the long seed parameter. The following code shows the changes:

    public class RandomDemo { // arbitrary example class name
        // lots of class related stuff may be here...
    
        // still inside the class scope...
        // private is a good idea unless an external method needs to change it
        private Random randnum = new Random(123456789L);
        // the seed guarantees it will always produce the same sequence
        // of pseudo-random values when the next methods get called
        // for unpredicable sequences, use the following constructor instead:
        // private Random randnum = new Random();
    
        // lots of code may be here...
    
        // publicly exposed instance method for getting random number
        // from a sequence determined by seed 123456789L
        // in the range from 0 through i-1
        public int randnum(int i) {
            // don't set the seed in here, or randnum will return the exact same integer
            // for the same value of i on every method call
            // nextInt(i) will give the next value from randnum conforming to range i
            return randnum.nextInt(i);
        } // end randnum
    
        // lots of more code may be here...
    
    } // end class RandDemo
    

    The above will give you an exact solution to your exact problem, as stated. However, using a mandatory seed seems unusual, given what it does.

    The following explains more about Random, seeds for Random and why there is a provision for supplying a seed.

    Random has two constructors:

    Random()
    

    and

    Random(long seed)
    

    and an instance method

    setSeed(long seed)
    

    that all affect the sequence of numbers obtained from a Random instance. The instance method,

    setSeed(long seed)
    

    sets the Random object to the same state it would have been in had it been just instanced with the same seed as the constructor argument. Only the low-order 48 bits of a seed value get used.

    If a Random object is instanced without a seed, the seed will be the same as the system time in milliseconds. This ensures that, unless two Random objects are instanced in the same millisecond, they will produce different pseudo-random sequences. Only the low order 48 bits of the seed value gets used. This causes unpredictable pseudo-random sequences. It is not necessary and wasteful of computing resources to get a new instance of Random every time one calls a next method.

    Random’s seed parameters are provided so that one may instance a Random object that produces a sequence that is repeatable. For a given seed, the sequence of values in next methods are guaranteed to be the same sequence whenever that seed is used. This is useful for testing software that is going to use pseudo-random sequences where results have to be predicable and repeatable. It is not useful for creating different unpredictable pseudo-random sequences in operation.

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

Sidebar

Related Questions

Possible Duplicate: Java: Always override equals? should I override equals function for any class
Possible Duplicate: Java: generating random number in a range I want to generate a
Possible Duplicate: Java: generating random number in a range How do I generate a
Possible Duplicate: Generating random number in a range with Java My code is generating
Possible Duplicate: Java: generating random number in a range I want to generate random
Possible Duplicate: Generating random number in a range with Java double x = //Random
Possible Duplicate: Generating random number in a range with Java How can I generate
Possible Duplicate: Java: generating random number in a range I need a little help.
Possible Duplicate: Java: generating random number in a range How do I generate a
Possible Duplicate: Java HTTP getResponseCode returns 200 for non-existent URL Hello, my goal is

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.