Character Impacts Culture

The culture of your team is the life-blood of your organization. Establishing the right culture influences every single department. You can assemble the most talented people and sell the greatest…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Generating Pseudo Random Numbers With Java 17

pseudo random number

Hello Readers and Developers we welcome you all to this article on Generating Pseudo Random Numbers with Java 17. There are many updates to the API for generating random numbers in the latest Java 17 release. With Java 17 API update, new interface types have been introduced, as well as methods to easily list, find and instantiate generator factories. Also a new set of random number generator implementations is now available.

In this article we will generate random numbers with old and new APIs with full code and explanation. So without further delay let us start by understanding the old API first.

In old API we use Random class to generate pseudo random numbers. Random class belongs to java.util package. Below is the complete code for it,

In the above code we can see that the Random class object is created and then the nextInt() method is invoked to get the random number in the specified range. The constructor sets the seed of the random number generator to a value that is very likely distinct from any other invocation. Below is the output of this example,

All instances of Random class are thread safe but concurrent usage may result in poor performance. To handle this ThreadLocalRandom class is used. Also the cryptographically secure SecureRandom class is available in the old API.

Now Let us take a look at the new API,

The new API provides an improved overall design with new interface types and generator implementations. With the old API, switching between Different generators was difficult. In the new API support for stream based processing is added. The new API also removes duplicate code in classes.

The new root interface RandomGenerator provides a uniform API for all existing and new random number generators.

A factory class for generating multiple random number generators of a specific algorithm was added in the new API.

Now below is the code for generating pseudo random numbers with new API,

In the above code we are using RandomGenerator to create pseudo random numbers. Here we are passing the name of the random number generator as parameter.

Below is the output for this random number generation code.

As they don’t have to be thread-safe, the new generator implementations perform faster compared to Random (old API).

In this article we have seen the generation of pseudo random numbers by using the old API and java 17 API. Java 17 added new interface types as well as new generator implementations. We hope you understood these features. Wishing you all the best in coding.

Thank you for reading this article. Take care and See you in the next article.

Add a comment

Related posts:

Why I am Unable to Write This Week

The image of the young woman and her boyfriend running with their 18 — month old injured baby wrapped in a blanket into the hospital in Ukraine keeps coming back to me. Their blood-stained clothes…

Forbidden Love Part 4

My name is Sunny Alexander-Johnson, and I’m Henry James, and we’re writers for Dark Sides of the Truth Magazine. After driving back to Dark Sides, we parted company then rejoined just outside the…

An Attempt at Wholeness

An article on how to cultivate wholeness in events with children and adults by working with four perspectives: head, heart, hand and spirit.