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
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

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.

John Vester user avatar by
John Vester
CORE ·
Jan. 30, 17 · Tutorial
Like (0)
Save
Tweet
Share
146.52K Views

Join the DZone community and get the full member experience.

Join For Free

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!

application

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Handling Virtual Threads
  • Integration: Data, Security, Challenges, and Best Solutions
  • Explaining: MVP vs. PoC vs. Prototype
  • Internal Components of Apache ZooKeeper and Their Importance

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: