Generating Random Numbers with Weighted Probability in Java

0
7

It’s been a while since my last Java programming post. I’ve been focusing on Python and Machine Learning tutorials recently, but Java remains my favorite programming language.

Random number selection with weighted probabilities is a common task in programming, especially when working with data that has a specific probability distribution. For example, you may have a list of letters with different probabilities of being selected, or a list of items with different probabilities of being chosen from a menu. In this post, we will explore how to implement a solution for this problem in Java using a simple and intuitive approach.

Random number selection

A random number is a number that is generated by a computer program or a hardware random number generator in a way that is unpredictable and does not follow a deterministic pattern. The purpose of generating random numbers is to provide a means of introducing an element of chance into a computer program or system.

There are many different ways to generate random numbers, and the quality and characteristics of the random numbers generated can vary significantly depending on the method used. In general, however, a good random number generator should produce numbers that are uniformly distributed over a wide range, are statistically independent of one another, and are not easily predictable.

To generate a random number in Java, you can use the java.util.Random class. For example, the following code generates a random number between two numbers:

This code generates a random integer between 0 and 10, inclusive. Each number has an equal probability of being chosen. To test this algorithm, we are going to create a simple proof-of-concept test.

Generating Random Numbers with Equal Probability

To test the code above, I am going to create a testing script. The code I am going to present here can be found in our Github repository.

The first thing I am going to do is to declare a class with the following attributes:

This class makes possible to create objects with letters, a counter and an specific probability. Now I am going to create a list of 10 letters:

To randomly select a letter, I created this method:

The method performs a loop “testCount” number of times. In each iteration of the loop, it generates a random integer between 0 and the size of the “letters” list minus 1. Then, it selects the “Letter” object at the index of the random number and increments its count by 1.

After the loop finishes executing, the method prints out the count and percentage of each “Letter” object. Finally, it resets the count of each “Letter” object to 0.

To test this code, you need to make something like this:

The following output is produced after running the random letter selection 100,000 times:

As we can see, each letter has been selected approximately 10% of the time, with the results fluctuating slightly above or below this value. This demonstrates that the random letter selection process is generating output with equal probability for each letter.

Generating Random Numbers with Weighted Probability in Java

Sometimes we may want to generate random numbers with probabilities that are not all equal. For example, we may want one letter to be selected more frequently than the others. In these cases, we can use weighted probabilities. This allows us to specify the probability of selecting a particular element, rather than all elements having an equal probability of being selected.

In the letter list that I created before I assigned a probability for each letter:

Now, we will create a method similar to the previous one, but the resulting percentages for each letter should be close to the probabilities that were set. Here is the method:

The result of running this code is:

As seen in the output, the selection frequency for each letter aligns with the probability that was previously set. This demonstrates that our algorithm effectively functions as a weighted random number generator.

Conclusion

The complete code with both examples can be downloaded from our Github Repository.

In conclusion, we have learned how to generate random numbers with equal probability and with weighted probability in Java using the java.util.Random class. We have also seen how to implement a random selection algorithm with weighted probabilities using a list of objects with a letter, a counter and a probability attribute.

We hope that this post has been helpful to you and if you have any questions or comments, please don’t hesitate to leave them below. Thank you for reading!

0 0 votes
Article Rating
Suscríbete
Notify of
guest

0 Comments
Inline Feedbacks
View all comments