Implementation of java.util.Random

K

Karrde712

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)?
 
D

Daniel Pitts

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?
 
R

Roedy Green

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.
 
A

Arne Vajhøj

Roedy said:
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
 
R

Roedy Green

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.
 
D

Daniel Pitts

Lew said:
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 :)
 
L

Lew

Daniel said:
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.
 
A

Arne Vajhøj

Roedy said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top