T
T
TostMaria2014-12-15 03:39:05
Java
TostMaria, 2014-12-15 03:39:05

How does random work in java?

I have a list of numbers. The minimum number in the list is 97, the maximum is 955.
There are 42 numbers in total.
I use the method "mathRandd=(int)(Math.random() * 1000) + 1", the same numbers appear suspiciously often. Those. several basic numbers (2-3) drop out very often, and others are already "randomly" to them. Moreover, if the program initially "selected" several numbers, then until I replace the entire list, it is these numbers that will be the base ones all the time (that is, at each start, the selection of frequently falling numbers is saved).
The question is - is it harmful to program at night, or is there an explanation and is it better to use some other random?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan Starkov, 2014-12-15
@icelaba

Usually such observations deceive you, so it’s better not to trust yourself and your eyes,
build an output histogram, and then if it differs very much from a uniform distribution, draw conclusions, but I dare to assure Java Math.random() will not differ with a large number of runs .
Although of course Math.random is a bad random number generator, but not as good as you describe.

N
Nikolai Pavlov, 2014-12-15
@gurinderu

I would advise you to still use commons.apache.org/proper/commons-math/apidocs/org...
Math.random() is sometimes difficult to describe complex cases of randomization.

Y
yuraminsk, 2014-12-17
@yuraminsk

there is java.security.SecureRandom - probably the best option. There is just Random, there is ThreadLocalRandom - it works even faster than Random - maybe these two work better than Math.random

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question