Generating a Random Number in Java From Atmospheric Noise
Learn more about how you can generate random numbers in Java using atmospheric noise.
Join the DZone community and get the full member experience.Join For Free
When it comes to generating a random number in Java, we have several options. However, I doubt any are as intriguing or as random as what you'll learn about in this post. So, if you're interested in how to generate a truly random number in Java using earth's atmospheric noise, then read on...
Java Random Number | Atmospheric Noise
After reading many posts about generating random numbers in Java over the years, I noticed that most finished off by dropping a reference to an online service at random.org, which generates randomness via atmospheric noise!
I used to brush it off with a "hmm, cool" until one day I read it again and thought ... wouldn't it be nice to create a Java class that actually tried to do that? So, I did! This gave birth to the AtmosphericRandom Java class.
First off, random.org is awesome! The site is used for lottery drawings and online gaming, among other use cases. The site allows you to generate random values based on atmospheric noise. What is atmospheric noise you ask? It's radio noise caused by natural atmospheric processes, primarily lightning discharges in thunderstorms.
In other words, this totally unpredictable natural phenomenon can be measured and used to derive random values — that's good for us! The random.org site allows you to generate the following random values:
- Integers (what this post covers — via
nextIntmethod, which internally calls
generateIntegersat random.org )
- Integer Sequences
- Decimal Fractions
You can create a free developer account in which an API key is generated for you to use to access the basic API. I opted to use their latest JSON RPC 2.0 API — sorry, no RESTful API available.
By programming an HTTP Client using Apache's HttpComponents API for JSON data binding and using Spring Boot 2.0 as fairy dust, I was able to put a proof of concept together in order to generate a truly random integer.
If you're interested in checking out the MVP Java YouTube video tutorial, then click here.
Unless you have special equipment connected to your computer to measure the atmospheric noise, you'll need an API key to get started. I included mine in the application.properties file below but will deactivate it by the time you read this since I don't want ya'll to use up all my free requests!
When you sign up, you'll get your own API key, so just replace yours in there next to property
Of course, we need to declare our entry point for this Spring Boot application with
@SpringBootApplication, and then, the dependency injects our starting point (application.class), which we see below is being injected via
Atmospheric Random nextInt
Generating random integers via class
AtmpsphericRandom is similar in using Java's Random or
SecureRandom in the sense that when you get a reference to it, all you'll have to do is call the
AtmpsphericRandom is dependency injected into the constructor of the application class below by way of Spring Java Configuration (shown later). You can see how easy it is to use
AtmpsphericRandom in the
startApplication() method below.
I set up defaults to generate a single random integer with base 10 and within range: 1-1000 when using
nextInt() with no arguments. You can see the JSON request and response below with the application printing out a single random integer as requested by default. The values are within the responses "data" array element shown below.
Customizing Request for Random Integer | nextInt
You can also use the overloaded methods of
nextInt( .... ) in order to override the defaults. The following
nextInt (5, 1, 2000) is specifying that 5 random number integers be requested, the minimum range is 1 and the maximum range is 2000.
The overloaded versions return an array of integers since multiple (5) random integer values are requested this time.
The following is the JSON request and response console output ending with the application printing out the 5 random integer values requested.
I'll be the first to admit that this is not the fastest way to get a random number in Java, but nothing stops you from making a single request for as many random integers as you see fit as shown above. There is, unfortunately, an added overhead to making an HTTP request to the random.org service but that's the price you have to pay to get a random integer from the heavens!
AtmosphericRandom class requires some building to get it off the ground and running. Thankfully, Spring Java configuration really helps out here by allowing us to dependency inject.
- CloseableHttpClient: A Http Client from Apache's
HttpComponentsAPI used to send and receive HTTP request/responses
- HttpPostFactory: Hides all the creation details needed in mapping the Java Object
RandomRequestinto a JSON request wrapped into an HTTP POST request
- RandomMapper: Extends Jacksons
ObjectMapperin order to provide a custom mapping from/to JSON/Java Objects (
RandomResponse, which are not shown in this post —check out the source on GitHub)
Once those three dependencies described above are built for us by Spring Java
@Configuration, they are then used to create the
@Bean. Our application
@Bean then receives
AtmosphericRandom in its constructor for us to use in the demo.
I walk through a test run of the application here on YouTube.
There's something just cool in knowing that you can generate a random number off of atmospheric noise, right?! Although this demo just scratches the surface in allowing you to retrieve random integers from random.org, it can be extended to request other types supported by the API. I invite you to go to MVP Java's GitHub account here to play around with the source code — just don't forget to get your own API key!
Published at DZone with permission of Andy Luis. See the original article here.
Opinions expressed by DZone contributors are their own.