Java Math.random() How random is it?

astralmaster

I am working on a project that needs to generate two random numbers from a given range (both of them at the same time, one after another) and check if they are equal to each other - if they are, proceed executing other code; if they aren't - generate the numbers again. Now my question is, if we have a range [0;10], and the first randomly generated number turned out to be 5, is the probability of the second number also being 5 as good as any other number? Specifically, does Math.random() have any "defense" against generating same number if it is called twice consecutively? or it "tries" to not generate the same number?

Jim Garrison

Generating the same number in the range [0,10] twice in succession is a perfectly valid occurrence for any random number generator. If it took any steps to prevent that it wouldn't be random.

On any invocation, the chances of any individual number being chosen should be 1:11, and each choice should be independent of previous choices, so the chances that in a pair the second number matches the first is 1 in 11.

As to how random Math.random() is, it's pseudo-random, meaning it uses an algorithm to generate a series of evenly distributed numbers starting with a "seed" value. It's not suitable for cryptography but quite good for simulations and other non-cryptographic uses.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Math.random() in Java, how is this possible?

From Dev

Java Math.random not random in while loop

From Dev

Java Math.random period

From Dev

Java Math.random() concern

From Dev

how to create a java function that makes random math problems.

From Dev

How does Math.random() EXACTLY work in Java?

From Dev

How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

From Dev

How do you generate a random number with equal probability using Math.random() in Java

From Dev

How do you generate a random number with equal probability using Math.random() in Java

From Dev

How to display a specific range of random numbers in java using Math.random

From Dev

Random Math Java Program Showing Incorrect Answers

From Dev

Java Math.random returning 0?

From Dev

Java VS Matlab : Math.random() and rand

From Dev

How to place value at random locations in an array using Math.random?

From Dev

Javascript Math.random() not *that* random…?

From Dev

Math.Random is not so random

From Dev

How to make random float in swift with math operations

From Dev

How to specify range on math.random?

From Dev

how to make Math.random round to a number

From Dev

How to Print Random number in a Math Table?

From Dev

Math.round (), How to with a random number?

From Dev

Is it possible for Math.random() === Math.random()

From Dev

In Java 8 why we cannot convert Math.random() to Math::random using method references

From Dev

Math.random not working

From Dev

Math & JavaScript | Random chances

From Dev

Arrays Random Multiplication Math

From Dev

Math.random Scenario

From Dev

Math.random number of random bits

From Dev

Math.Random() To pick random array

Related Related

HotTag

Archive