📜 ⬆️ ⬇️

Random number generation using Random.org

He who tries to generate random numbers by arithmetic means, of course, lives in sin.
- John von Neumann

There is such a good service random.org , which has already been mentioned many times in Habre. 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 what is better and why. This article describes a simple library for using the site API.

Random.org


Random.org has a lot of useful functions that use random number generation: throwing coins, dice, shuffling cards, getting a lottery combination, generating sounds, bitmaps and much more. There is also a custom generation for a given distribution. In principle, all this is not difficult, but it is interesting that the generation takes place using atmospheric noise and in some magical way it allows you to get a better random than Random.nextInt (). Then I thought it would be nice to have in store a library with such an API and decided to write it.
')

Search


Before you write, you need to search, maybe someone has already done this. Yes. Did.

In general, I decided to write my own.

API


Random.org provides a primitive HTTP GET API, however nothing more is needed. There are only 4 types of operations.

Integer generator

Generates random integers in the specified range. For example, this is a query for throwing 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 the specified range. Essentially what Collections.shuffle () does. For example, this is a request for mixing a deck of cards:
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, Upper case). So, for example, you can generate a nickname for your character password:
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, give a million free bits per day. This is more than enough. And in order to find out how much is left at the following link:
http://www.random.org/quota/?format=plain
If you clicked on the three previous links, then you have already spent ~ 1500 bits.

Errors

In case of success of the generation, the server returns the code 200, failure - code 503. That's all the errors.

For this API, a library of five classes in Java was written, in which a call to 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 view the sources and download the lib with the original name randomorg (6 kilobytes).

Source: https://habr.com/ru/post/145283/


All Articles