Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How does java.util.Random work?

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How does java.util.Random work?

    long seed = 314159265;
    Random rand = new Random(seed);
    int a = rand.nextInt();        
     
    seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
    int b = (int) (seed >>> (48 - 32));

    Why is a not equal to b?

    Here is the source code for nextInt() and next():

    public int nextInt()
    {
        return next(32);
    }
     
     
    protected synchronized int next(int bits)
    {
        seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
        return (int) (seed >>> (48 - bits));
    }
    Last edited by Samkli; October 7th, 2014 at 04:10 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: How does java.util.Random work?

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How does java.util.Random work?

    Well a:

    int a = rand.nextInt();

    and b:

    int b = (int) (seed >>> (48 - 32));

    That's why they're not equal.

    nextInt read next input, not next defined variable.

  4. #4
    Junior Member
    Join Date
    Oct 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How does java.util.Random work?

    Quote Originally Posted by TheBeginning View Post
    nextInt read next input, not next defined variable.
    Could you explain what you mean by this?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 832 Times in 772 Posts
    Blog Entries
    5

    Default Re: How does java.util.Random work?

    Look at the javadocs for the constructor Random(int seed)

Similar Threads

  1. ASSIGNMENT INVOLVING RANDOM UTIL
    By michael c in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2014, 04:47 AM
  2. Replies: 4
    Last Post: April 16th, 2013, 06:50 AM
  3. How exactly does random block generation work in 2D/3D worlds?
    By vividMario52 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 24th, 2013, 03:27 PM
  4. Can a for loop work with random number?
    By humdinger in forum Loops & Control Statements
    Replies: 7
    Last Post: January 10th, 2010, 09:06 PM
  5. Replies: 8
    Last Post: February 24th, 2009, 03:04 PM