DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Generating a Random Number in Java From Atmospheric Noise

Generating a Random Number in Java From Atmospheric Noise

Learn more about how you can generate random numbers in Java using atmospheric noise.

Andy Luis user avatar by
Andy Luis
·
May. 21, 19 · Tutorial
Like (9)
Save
Tweet
Share
10.44K Views

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 nextInt method, which internally calls generateIntegers at random.org )
  • Integer Sequences
  • Decimal Fractions
  • Strings
  • UUIDs
  • Blobs

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.

AtmosphericRandom Application

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.

Image title

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 @Autowired.

Image title

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 thestartApplication() 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.

Image title

The following is the JSON request and response console output ending with the application printing out the 5 random integer values requested.

Image title

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!

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 an HTTP POST request
  • RandomMapper: Extends Jacksons ObjectMapper in order to provide a custom mapping from/to JSON/Java Objects (RandomRequestand RandomResponse, which are not shown in this post —check out the source on GitHub)

Image title

Once those three dependencies described above are built for us by Spring Java @Configuration, they are then used to create the AtmosphericRandom Spring@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.

Conclusion

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!

Java (programming language) Spring Framework

Published at DZone with permission of Andy Luis. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Introduction to Containerization
  • NoSQL vs SQL: What, Where, and How
  • Application Architecture Design Principles
  • Stop Using Spring Profiles Per Environment

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: