Random double value java

Class Random

An instance of this class is used to generate a stream of pseudorandom numbers; its period is only 2 48 . The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald E. Knuth, The Art of Computer Programming, Volume 2, Third edition: Seminumerical Algorithms , Section 3.2.1.)

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random . Java implementations must use all the algorithms shown here for the class Random , for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.

Читайте также:  Test local html file

The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.

Many applications will find the method Math.random() simpler to use.

Instances of java.util.Random are threadsafe. However, the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance. Consider instead using ThreadLocalRandom in multithreaded designs.

Instances of java.util.Random are not cryptographically secure. Consider instead using SecureRandom to get a cryptographically secure pseudo-random number generator for use by security-sensitive applications.

Источник

Class Random

An instance of this class is used to generate a stream of pseudorandom numbers; its period is only 2 48 . The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald E. Knuth, The Art of Computer Programming, Volume 2, Third edition: Seminumerical Algorithms , Section 3.2.1.)

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random . Java implementations must use all the algorithms shown here for the class Random , for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.

The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.

Many applications will find the method Math.random() simpler to use.

Instances of java.util.Random are threadsafe. However, the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance. Consider instead using ThreadLocalRandom in multithreaded designs.

Instances of java.util.Random are not cryptographically secure. Consider instead using SecureRandom to get a cryptographically secure pseudo-random number generator for use by security-sensitive applications.

Источник

Java – Random Long, Float, Integer and Double

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

We rely on other people’s code in our own work. Every day.

It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.

Lightrun is a new kind of debugger.

It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.

Learn more in this quick, 5-minute Lightrun tutorial:

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

We’re looking for a new Java technical editor to help review new articles for the site.

This quick tutorial will illustrate how to generate a long first using plain Java and using the Apache Commons Math library.

This article is part of the “Java – Back to Basic” series here on Baeldung.

1. Generate an Unbounded Long

Let’s start with generating a Long:

@Test public void givenUsingPlainJava_whenGeneratingRandomLongUnbounded_thenCorrect()

2. Generate a Long Within a Range

2.1. Random Long With Plain Java

Next – let’s look at creating a random bounded Long – that is, a Long value within a given range or interval:

@Test public void givenUsingPlainJava_whenGeneratingRandomLongBounded_thenCorrect()

2.2. Random Long With Apache Commons Math

Let’s take a look at generating the random Long with a cleaner API and Commons Math:

@Test public void givenUsingApacheCommons_whenGeneratingRandomLongBounded_thenCorrect()

3. Generate an Unbounded Integer

Let’s move right on to generating a random Integer with no bounds:

@Test public void givenUsingPlainJava_whenGeneratingRandomIntegerUnbounded_thenCorrect()

As you can see, it’s pretty close to generating a long.

4. Generate an Integer Within a Range

4.1. Random Integer With Plain Java

Next – a random integer within a given range:

@Test public void givenUsingPlainJava_whenGeneratingRandomIntegerBounded_thenCorrect()

4.2. Random Integer With Commons Math

And the same with Common Math:

@Test public void givenUsingApache_whenGeneratingRandomIntegerBounded_thenCorrect()

5. Generate an Unbounded Float

Now, let’s go over generating random floats – first unbounded:

@Test public void givenUsingPlainJava_whenGeneratingRandomFloatUnbouned_thenCorrect()

6. Generate a Float Within a Range

6.1. Random Float With Plain Java

And a bounded random float:

@Test public void givenUsingPlainJava_whenGeneratingRandomFloatBouned_thenCorrect()

6.2. Random Float With Commons Math

Now – a bounded random float with Commons Math:

@Test public void givenUsingApache_whenGeneratingRandomFloatBounded_thenCorrect()

7. Generate an Unbounded Double

7.1. Random Unbounded Double With Plain Java

Finally – we’re going to generate random double values – first, with the Java Math API:

@Test public void givenUsingPlainJava_whenGeneratingRandomDoubleUnbounded_thenCorrect()

7.2. Random Unbounded Double With Commons Math

As well as a random double value with the Apache Commons Math library:

@Test public void givenUsingApache_whenGeneratingRandomDoubleUnbounded_thenCorrect()

8. Generate a Double Within a Range

8.1. Random Bounded Double With Plain Java

In this example, let’s take a look at a random double generated within an interval – with Java:

@Test public void givenUsingPlainJava_whenGeneratingRandomDoubleBounded_thenCorrect()

8.2. Random Bounded Double With Commons Math

And lastly – a random double within an interval, using the Apache Commons Math library:

@Test public void givenUsingApache_whenGeneratingRandomDoubleBounded_thenCorrect()

And there you have it – quick and to the point examples of how to generate both unbounded and bounded values for the most common numerical primitives in Java.

9. Conclusion

This tutorial illustrated how we could generate random numbers either bound or unbound, using different techniques and libraries.

As always, the implementation of all of these examples and snippets can be found in the GitHub project. This is a Maven-based project so it should be easy to import and run.

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

Источник

Оцените статью