Random Number Generation with Random.org
He who tries to generate random numbers by arithmetic methods, of course, lives in sin.
- John von Neumann
There is such a good service random.org , which has already been mentioned more than once on the hub The main task of the site is the generation of random numbers using atmospheric noise. On the same site you can find test results and comparisons of random and pseudo-random generators with explanations of which is better and why. This article describes a simple library for using the site API.
Random.org has a lot of useful features that use random number generation: tossing coins, dice, shuffling cards, getting a lottery combination, generating sounds, bitmaps and much more. There is also custom generation for a given distribution. In principle, all this is not difficult, but the fact that the generation occurs using atmospheric noise is interesting and this somehow magically allows us to get a better random than Random.nextInt (). Then I thought that it would be nice to have a library in stock with such an API and decided to write it.
Before you write, you need to look, maybe someone has already done this. Yes. Did.
In general, I decided to write my own.
Random.org provides a primitive HTTP GET API, but nothing more is needed. There are only 4 types of operations.
Generates random integers in a given range. For example, this is the request to roll two dice:
http://www.random.org/integers/?num=2&min=1&max=6&col=1&base=10&format=plain&rnd=new
Generates a sequence with all unique integers in a given range. Essentially what Collections.shuffle () does. For example, the request for shuffling a deck of cards looks like this:
http://www.random.org/sequences/?min=1&max=52&col=1&format=plain&rnd=new
Generates a random string of a given size with the ability to select a set of characters (numbers, lover case, apper case). So, for example, you can generate anickname for your character's password:
http://www.random.org/strings/?num=1&len=12&digits=on&upperalpha=on&loweralpha=on&unique=on&format=plain&rnd=new
Well, as you already understood, all this is not free. Although not, they give a million free bits per day. This is more than enough. And in order to find out how much is left, go to the following link:
http://www.random.org/quota/?format=plain
If you clicked on the three previous links, then you already spent ~ 1500 bits.
If the generation is successful, the server returns a code of 200, failure - code 503. That's all the errors.
For this API, a library of five classes was written in Java, in which the call of all the above methods in a simple and understandable form.
Like all. On github you can see the source and download the lib with the original name randomorg (6 kilobytes).
- John von Neumann
There is such a good service random.org , which has already been mentioned more than once on the hub The main task of the site is the generation of random numbers using atmospheric noise. On the same site you can find test results and comparisons of random and pseudo-random generators with explanations of which is better and why. This article describes a simple library for using the site API.
Random.org
Random.org has a lot of useful features that use random number generation: tossing coins, dice, shuffling cards, getting a lottery combination, generating sounds, bitmaps and much more. There is also custom generation for a given distribution. In principle, all this is not difficult, but the fact that the generation occurs using atmospheric noise is interesting and this somehow magically allows us to get a better random than Random.nextInt (). Then I thought that it would be nice to have a library in stock with such an API and decided to write it.
Search
Before you write, you need to look, maybe someone has already done this. Yes. Did.
- Simple Random.Org Java Api is a simple library with a method for generating integers, but it pulls the Apache HTTP Client dependency , as many as 658 kilobytes.
- Java TRNG client - everything is more serious here. 40 kilobytes and number generation using two (!) Sites. Yes, the drawback is that the lib was created for cryptography, because there it is handling bits, bytes and in general everything is complicated.
In general, I decided to write my own.
API
Random.org provides a primitive HTTP GET API, but nothing more is needed. There are only 4 types of operations.
Integer generator
Generates random integers in a given range. For example, this is the request to roll two dice:
http://www.random.org/integers/?num=2&min=1&max=6&col=1&base=10&format=plain&rnd=new
Sequence generator
Generates a sequence with all unique integers in a given range. Essentially what Collections.shuffle () does. For example, the request for shuffling a deck of cards looks like this:
http://www.random.org/sequences/?min=1&max=52&col=1&format=plain&rnd=new
String Generator
Generates a random string of a given size with the ability to select a set of characters (numbers, lover case, apper case). So, for example, you can generate a
http://www.random.org/strings/?num=1&len=12&digits=on&upperalpha=on&loweralpha=on&unique=on&format=plain&rnd=new
Quota checker
Well, as you already understood, all this is not free. Although not, they give a million free bits per day. This is more than enough. And in order to find out how much is left, go to the following link:
http://www.random.org/quota/?format=plain
If you clicked on the three previous links, then you already spent ~ 1500 bits.
Mistakes
If the generation is successful, the server returns a code of 200, failure - code 503. That's all the errors.
For this API, a library of five classes was written in Java, in which the call of all the above methods in a simple and understandable form.
// бросаем кубики
IntegerGenerator ig = new IntegerGenerator();
ig.generate(1, 6, 2);
// тасуем карты
SequenceGenerator sg = new SequenceGenerator();
sg.generate(1, 52);
// новый пароль
StringGenerator strg = new StringGenerator();
strg.generate(12, 1, true, true, true, true);
// сколько бит осталось
QuotaChecker qc = new QuotaChecker();
qc.quota();
Like all. On github you can see the source and download the lib with the original name randomorg (6 kilobytes).