Implementation of java.util.Random

Discussion in 'Java' started by Karrde712@gmail.com, Feb 21, 2008.

  1. Guest

    Is java.util.Random.nextInt() guaranteed to implement the same
    pseudorandom algorithm on all platforms?

    In simpler terms, if I give the same seed to the constructor of Random
    on several different platforms, will it produce exactly the same
    sequence on all JREs (Sun, Apple, JRockit and gcj at least)?
     
    , Feb 21, 2008
    #1
    1. Advertising

  2. Daniel Pitts Guest

    wrote:
    > Is java.util.Random.nextInt() guaranteed to implement the same
    > pseudorandom algorithm on all platforms?
    >
    > In simpler terms, if I give the same seed to the constructor of Random
    > on several different platforms, will it produce exactly the same
    > sequence on all JREs (Sun, Apple, JRockit and gcj at least)?

    Did you *try* reading the Java docs?
    <http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html>
    > If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.




    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Feb 21, 2008
    #2
    1. Advertising

  3. Roedy Green Guest

    On Thu, 21 Feb 2008 07:36:54 -0800 (PST), ""
    <> wrote, quoted or indirectly quoted someone who
    said :

    >Is java.util.Random.nextInt() guaranteed to implement the same
    >pseudorandom algorithm on all platforms?
    >
    >In simpler terms, if I give the same seed to the constructor of Random
    >on several different platforms, will it produce exactly the same
    >sequence on all JREs (Sun, Apple, JRockit and gcj at least)?


    I believe the code is written in pure Java. You can check that by
    looking in src.zip. If it is, you have that guarantee. The method
    that is used to generate a very random seed for cryptography that
    collects entropy is likely native though.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Feb 21, 2008
    #3
  4. Arne Vajhøj Guest

    Roedy Green wrote:
    > I believe the code is written in pure Java. You can check that by
    > looking in src.zip. If it is, you have that guarantee.


    No.

    Java is a specification. Even though SUN has created a pure
    Java implementation then IBM/BEA/Oracle/Apache/GNU/whoever
    may still make their own implementation. As long as it follows
    the specification it is OK.

    > The method
    > that is used to generate a very random seed for cryptography that
    > collects entropy is likely native though.


    Could be.

    But I seem to recall that it gets its external randomness
    by starting threads and measuring times for that.

    Actually that likely involves native code as well, but
    not native random code.

    Arne
     
    Arne Vajhøj, Feb 21, 2008
    #4
  5. Lew Guest

    Daniel Pitts wrote:
    > wrote:
    >> Is java.util.Random.nextInt() guaranteed to implement the same
    >> pseudorandom algorithm on all platforms?
    >>
    >> In simpler terms, if I give the same seed to the constructor of Random
    >> on several different platforms, will it produce exactly the same
    >> sequence on all JREs (Sun, Apple, JRockit and gcj at least)?

    > Did you *try* reading the Java docs?
    > <http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html>


    Better yet, access that information via the link to the current version of Java
    <http://java.sun.com/javase/6/docs/api/java/util/Random.html>

    not one that is due to retire this year
    <http://java.sun.com/j2se/1.4.2/>

    In this particular case the Javadocs are identical, but that cannot always be
    counted on.

    --
    Lew
     
    Lew, Feb 22, 2008
    #5
  6. Roedy Green Guest

    On Thu, 21 Feb 2008 18:36:53 -0500, Arne Vajhøj <>
    wrote, quoted or indirectly quoted someone who said :

    >But I seem to recall that it gets its external randomness
    >by starting threads and measuring times for that.


    I found this code in SecureRandom.

    private static String getPrngAlgorithm() {
    List provs = Providers.getProviderList().providers();
    for (Iterator t = provs.iterator(); t.hasNext();) {
    Provider p = (Provider)t.next();
    for (Iterator u = p.getServices().iterator();
    u.hasNext();) {
    Service s = (Service)u.next();
    if (s.getType().equals("SecureRandom")) {
    return s.getAlgorithm();
    }
    }

    So you don't know much about how it works. It depends on what
    providers are registered.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Feb 22, 2008
    #6
  7. Daniel Pitts Guest

    Lew wrote:
    > Daniel Pitts wrote:
    >> wrote:
    >>> Is java.util.Random.nextInt() guaranteed to implement the same
    >>> pseudorandom algorithm on all platforms?
    >>>
    >>> In simpler terms, if I give the same seed to the constructor of Random
    >>> on several different platforms, will it produce exactly the same
    >>> sequence on all JREs (Sun, Apple, JRockit and gcj at least)?

    >> Did you *try* reading the Java docs?
    >> <http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html>

    >
    > Better yet, access that information via the link to the current version
    > of Java
    > <http://java.sun.com/javase/6/docs/api/java/util/Random.html>
    >
    > not one that is due to retire this year
    > <http://java.sun.com/j2se/1.4.2/>
    >
    > In this particular case the Javadocs are identical, but that cannot
    > always be counted on.
    >

    Well, blame either Google for not updating the link, or Sun for not
    re-using a URL for the modern version :). Don't blame me for not going
    through the extra hassle of searching for the same sentence in a
    different version :)

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Feb 22, 2008
    #7
  8. Lew Guest

    Daniel Pitts wrote:
    > Well, blame either Google for not updating the link, or Sun for not
    > re-using a URL for the modern version :). Don't blame me for not going
    > through the extra hassle of searching for the same sentence in a
    > different version :)


    In this case Sun is my suspect. I have found several obsolete links from
    their own site to their own site.

    It would help if they kept a more consistent URL scheme, but as they've
    transformed their idea of version numbers, they've rearranged URL segments.
    The API docs changed from "/j2se/1.4.2/" to "/javase/6/". Other doc links
    changed in ways that are less obvious.

    --
    Lew
     
    Lew, Feb 22, 2008
    #8
  9. Arne Vajhøj Guest

    Roedy Green wrote:
    > On Thu, 21 Feb 2008 18:36:53 -0500, Arne Vajhøj <>
    > wrote, quoted or indirectly quoted someone who said :
    >> But I seem to recall that it gets its external randomness
    >> by starting threads and measuring times for that.

    >
    > I found this code in SecureRandom.

    ....
    > So you don't know much about how it works. It depends on what
    > providers are registered.


    That is basically what I told you in the post you replied to.

    The stuff I am talking about is in:
    sun.security.provider.SeedGenerator.ThreadedSeedGenerator

    And is obviously not something one should rely on.

    But it demonstrates that it is possible to get the
    entropy in pure Java.

    Arne
     
    Arne Vajhøj, Feb 24, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elmar Baumann
    Replies:
    0
    Views:
    611
    Elmar Baumann
    Feb 2, 2004
  2. Replies:
    2
    Views:
    490
    Andrew Thompson
    Jan 2, 2005
  3. Sakagami Hiroki

    java.util.Random.nextInt() thread safety

    Sakagami Hiroki, Aug 29, 2006, in forum: Java
    Replies:
    5
    Views:
    9,973
    Eric Sosman
    Aug 29, 2006
  4. globalrev
    Replies:
    4
    Views:
    771
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,175
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page