Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Building a Random Generator

DZone's Guide to

Building a Random Generator

Want to randomly sort a java.util.List object? Want to do it with a weighted value? This personal project might have wider reaching uses, and it's going to be open sourced.

· Java Zone
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

During the recent holiday break, I dusted off my IntelliJ application in order to do some development. In the process of rewriting an application I worked on a few years ago, I realized that a section of the application might be better served as its own library or component.

So, rather than focus on the core application, I decided to work on what I am calling the RandomGenerator. My goal, once finished, is for the random generator idea to provide value to other applications — outside of just my personal project.

About the Random Generator

In the README.md of my project, I introduced the random generator as follows:

The RandomGenerator project provides the ability to randomly sort a given java.util.List object, with the option to return a subset of the original list contents. A delimited String object can also be sorted, with the ability to return a subset of the original contents.

When using RandomGenerator with a List object, it is possible to introduce a weighted value to the randomization process by including a java.lang.Integer field called "rating" inside the List items being processed.

The project currently builds to a JAR file, which can be utilized by other Java projects, and also includes a command-line interface (CLI) which can be used to randomly sort a delimited String.

How the Random Process Works

The simplest implementation of RandomGenerator includes the following method:

public List<T> randomize(List<T> tList) {}


As an example, a List of String objects could contain the following items:

Boston, MA
Las Vegas, NV
Maui, HI
Miami, FL
New York, NY
San Diego, CA
Tampa, FL


If these values were in a List<String> called vacationSpots, we could call the RandomGenerator as follows:

List<String> randomizedList = RandomGenerator.randomize(vacationSpots);


One possible order of the randomizedList could be as shown below:

New York, NY
Las Vegas, NV
San Diego, CA
Boston, MA
Miami, FL
Maui, HI
Tampa, FL


As noted above, the RandomGenerator allows for weighting of the list values, provided there is a rating java.Lang.Integer within the List object. We can call the following (overloaded) randomize() method:

public List<T> randomize(List<T> tList, Boolean useRating) {}


Keeping things simple, let's assume a rating Integer was added to the list above:

Destination   Rating
Boston, MA      2
Las Vegas, NV   3
Maui, HI        5
Miami, FL       4
New York, NY    1
San Diego, CA   4
Tampa, FL       5


Assuming the vacationSpots List is a list of VacationSpot objects, we could call the randomize() method as listed below:

List<VacationSpot> randomizedList = RandomGenerator.randomize(vacationSpots, true);


One possible order of the randomizedList could be as shown below:

Maui, HI
Miami, FL
Tampa, FL
Las Vegas, NV
San Diego, CA
Boston, MA
New York, NY


The rating field favored the destinations with a higher rating, which caused the list to be ranked to better match end user's preferences.

Next Steps

I have the unit tests written and most of the documentation is complete. The code is uploaded to GitLab, and I plan to make it public soon. I chose GitLab so that I can explore the build options they have available.

Looking ahead, I plan to handle the following tasks:

  • Add build process to GitLab

  • Integrate Maven Release Plugin (if needed)

  • Store binaries by version online

  • Integrate with Maven Central

In my next article, I will give an update on how things are going and will share some of the source code used behind the methods noted above. It is my goal to provide something that others can use and not just my application which inspired the RandomGenerator. After all, according to Matt Mullenweg:

"When I first got into technology I didn't really understand what open source was. Once I started writing software, I realized how important this would be." - Matt Mullenweg

Have a really great day!

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,open source ,list ,randomness

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}