Class Random
- java.lang.Object
-
- java.util.Random
-
- All Implemented Interfaces:
- Serializable
- Direct Known Subclasses:
- SecureRandom, ThreadLocalRandom
public class Random extends Object implements Serializable
An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, 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 classRandom
. Java implementations must use all the algorithms shown here for the classRandom
, for the sake of absolute portability of Java code. However, subclasses of classRandom
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 aprotected
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 samejava.util.Random
instance across threads may encounter contention and consequent poor performance. Consider instead usingThreadLocalRandom
in multithreaded designs.Instances of
java.util.Random
are not cryptographically secure. Consider instead usingSecureRandom
to get a cryptographically secure pseudo-random number generator for use by security-sensitive applications.- Since:
- 1.0
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description Random()
Creates a new random number generator.Random(long seed)
Creates a new random number generator using a singlelong
seed.
-
Method Summary
Methods Modifier and Type Method and Description protected int
next(int bits)
Generates the next pseudorandom number.boolean
nextBoolean()
Returns the next pseudorandom, uniformly distributedboolean
value from this random number generator's sequence.void
nextBytes(byte[] bytes)
Generates random bytes and places them into a user-supplied byte array.double
nextDouble()
Returns the next pseudorandom, uniformly distributeddouble
value between0.0
and1.0
from this random number generator's sequence.float
nextFloat()
Returns the next pseudorandom, uniformly distributedfloat
value between0.0
and1.0
from this random number generator's sequence.double
nextGaussian()
Returns the next pseudorandom, Gaussian ("normally") distributeddouble
value with mean0.0
and standard deviation1.0
from this random number generator's sequence.int
nextInt()
Returns the next pseudorandom, uniformly distributedint
value from this random number generator's sequence.int
nextInt(int n)
Returns a pseudorandom, uniformly distributedint
value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.long
nextLong()
Returns the next pseudorandom, uniformly distributedlong
value from this random number generator's sequence.void
setSeed(long seed)
Sets the seed of this random number generator using a singlelong
seed.
-
'Dev. 자바 > API 및 이론' 카테고리의 다른 글
[JAVA API] java.lang.Math (0) | 2012.08.03 |
---|---|
[JAVA API] java.lang.StringBuilder (0) | 2012.08.03 |
[API] java.util.Calendar (0) | 2012.08.03 |
[API] java.lang.Integer (0) | 2012.08.03 |
[API] java.lang.String (0) | 2012.08.03 |