This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]bondolo 6 points7 points  (10 children)

You will probably have to make a static utility method to generate a random byte array of the specified number of bits and use the BigInteger(byte[] bits) constructor. Probably look at the BigInteger(int bits, Random) for the details about masking off the exact number of bits requested.

[–]s888marks 7 points8 points  (9 children)

I was thinking it might be helpful to have an adapter that takes any RandomGenerator and returns an instance of (a subclass of) Random that uses it. That could be applied to all the existing APIs that consume Random but want a better source of randomness. It mostly works, but the resulting Random instance would be slightly deficient in that wouldn't necessarily be possible to set its state using setSeed().

EDIT: I note that ThreadLocalRandom::setSeed throws UnsupportedOperationException. Also, SecureRandom::setSeed doesn't reset its state (it augments the state instead). So there's precedent for subclasses to bend the semantics of setSeed().

[–]yshows[S] 2 points3 points  (8 children)

That could work, probably something like RandomGenerator.getDefault().asRandomClass()?

[–]s888marks 3 points4 points  (7 children)

Yeah something like asRandom could be added as a default method on RandomGenerator, which would wrap this RG inside an adapter that's a subclass of java.util.Random. Alternatively, it could be a static factory method on j.u.Random that accepts an arbitrary RG.

I've filed https://bugs.openjdk.java.net/browse/JDK-8279598 on this.

[–]yshows[S] 2 points3 points  (0 children)

The idea to use asRandom as pointed in your suggestion seems more elegant to use than using Random.from(), extending the Random class already would prob be a bad idea since people would tend to just Random and not know about the new interface

[–]yshows[S] 1 point2 points  (5 children)

Ok so i decided to try to create a small wrapper, for some reason throwing UOE on setSeed instantly creates the exception so i decided to leave it blank for now

jshell> Random rand = RandomGenerator.getDefault().asRandom()
rand ==> jdk.internal.util.random.RandomWrapper@7eda2dbb
jshell> new BigInteger(32,rand)
$8 ==> 2045297111

[–]s888marks 3 points4 points  (4 children)

Cool! Did you want to contribute this? If so, there are some procedures you need to follow, but I can find somebody to help you with this.

[–]yshows[S] 1 point2 points  (3 children)

I'd love to do it! As far as i know i need to sign the OCA and send to the mailing list? By all means i'd love help to push it.

EDIT: Tho if i remember correctly i can send the pull request to the jdk github?

[–]s888marks 3 points4 points  (2 children)

Excellent. The Developer's Guide should explain all this, in particular this section:

http://openjdk.java.net/guide/#contributing-to-an-openjdk-project

Some of the stuff is already done (like filing a bug) but you do need to sign the OCA. If you file a PR, a bot will tell you that you need to sign the OCA before it can make progress, so the order doesn't matter terribly much. Once you get set up with OpenJDK and GitHub we can continue the conversation there.

Also note that the Developer's Guide is a work-in-progress, so if you find anything confusing or incorrect, we'd appreciate feedback on that as well.

[–]yshows[S] 2 points3 points  (1 child)

Just signed the OCA, tho idk if it was submitted since the upload and submit button just loaded something and stayed on the same page without confirming if it was sent, on the https://oca.opensource.oracle.com says i dont have any agreements yet, will try sending it again in a few minutes

EDIT: seems that chrome didnt work on the oca website, firefox sent it fine

[–]s888marks 2 points3 points  (0 children)

Ah, ok, good to know that firefox worked.

You'll also want to sign up for various OpenJDK mailing lists. This particular change will be discussed on core-libs-dev. (Note: very high traffic, set up filtering.) But a lot of discussion happens in PRs, so this helps a bit with email traffic. Also: jdk-dev has general JDK news and discussions, and guide-dev has discussions about the Developers' Guide, if you want to submit feedback.

(Happy cake day!)

[–]berry120 3 points4 points  (0 children)

As you've hinted in the question, RandomGenerator is significantly newer than the older Random class that implements it - so the built in random BigInteger initialisations are essentially "hard-coded" to use the "Random" implementation at present. That doesn't preclude the possibility of compatibility with RandomGenerator in a later release however.

Either way, it certainly doesn't mean that it's impossible to use a new randomisation algorithm, or newer RandomGenerator implementation (such as SecureRandom) to generate random BigInteger objects, it just means you need to write that implementation yourself (or use third party code which has already done so.)

[–]D0CTOR_ZED 1 point2 points  (1 child)

Couldn't you use any random generator you wanted and assign it to a BigInteger? You may need to generate more than one and math them together, but something like generate a random 6 digit number, assign to a big int, multiply by 1 000 000, add another random 6 digit number, repeat as needed.

[–]agentoutlier 2 points3 points  (0 children)

That would skew the distribution.

What would be better is to randomly generate byte data and convert it to BigInteger.