When it comes to generating a random number in Java, we have several options. However I doubt none are as intriguing or as random as what you’ll learn about in this post. If your interested in how to generate a truly random number in Java by using the earth’s atmospheric noise as it source, then read on …
Java Random Number From 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 use 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, 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 nextInt method which internally calls generateIntegers at random.org )
- Integer Sequences
- Decimal Fractions
You can create a free developer account in which an API key is generated for you use in order to access the basic API. I opted to use their latest JSON RPC 2.0 API – sorry no Restful API available.
By programming a HTTP Client using Apache’s HttpComponents API, Jackson 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 your interested in checking out the YouTube video tutorial then click below
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 ‘apiKey‘.
Of course we need to declare our entry point for this Spring Boot Application with @SpringBootApplication and then dependency inject our starting point (Application.class) which we see below is being injected via @Autowired.
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 nextInt() method.
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 setup 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.
Following is the JSON request & 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 a 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 | Spring Java Configuration
The 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 HttpComponents API used to send and receive HTTP request/responses
- HttpPostFactory: Hides all the creation details needed in mapping the Java Object RandomRequest into a JSON request wrapped into a HTTP POST request
- RandomMapper: Extends Jacksons ObjectMapper in order to provide custom mapping from/to JSON/Java Objects (RadomRequest & RandomResponse which are not shown in this post – check out the source on GitHub)
Once those 3 dependencies described above are built for us by Spring Java @Configuration they are then used to create the AtmosphericRandom Spring @Bean. Our Application @Bean than 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!
So how am I suppose to end off this post about generating a random number in Java now? I mean, we usually end off with dropping the random.org reference, right 😉