random class in java
Male avatar

LohithS viết ngày 11/08/2021

Your TIL title

An instance of Random class is used to generate a stream of pseudorandom numbers. Instances of java.util.Random are not cryptographically secure.

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.

random class declaration

public class Random extends Object implements Serializable

random class constructors

public Random() – creates a new random number generator.

public Random(long seed) – creates a new random number generator using a single long seed.

random methods

Here are few random methods java,

doubles() – returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive).

ints() – returns an effectively unlimited stream of pseudorandom int values.

longs() – returns an effectively unlimited stream of pseudorandom long values.

next(int bits) – generates the next pseudorandom number.

nextBoolean() – returns the next pseudorandom, uniformly distributed boolean value from this random number generator’s sequence.

nextBytes(byte[] bytes) – generates random bytes and places them into a user-supplied byte array.

nextDouble() – returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator’s sequence.

nextFloat() – returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator’s sequence.

nextGaussian() – returns the next pseudorandom, Gaussian (“normally”) distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator’s sequence.

nextInt() – java nextint returns the next pseudorandom, uniformly distributed int value from this random number generator’s sequence.

nextInt(int bound) – Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator’s sequence.

nextLong() – returns the next pseudorandom, uniformly distributed long value from this random number generator’s sequence.

setSeed(long seed) – Sets the seed of this random number generator using a single long seed.

Let’s see random class java example.

import java.util.Random;
public class JavaRandomClassExample
{
   public static void main(String[] args)
   {
      Random rand = new Random();
      System.out.println(rand.nextInt(10));
      System.out.println(rand.nextBoolean());
      System.out.println(rand.nextDouble());
      System.out.println(rand.nextFloat());
      System.out.println(rand.nextGaussian());
      byte[] bytes = new byte[10];
      rand.nextBytes(bytes);
      System.out.printf("[");
      for(int a = 0; a < bytes.length; a++)
      {
         System.out.printf("%d ", bytes[a]);
      }
      System.out.printf("]\n");
      System.out.println(rand.nextLong());  
      System.out.println(rand.nextInt());
      long seed = 55;
      rand.setSeed(seed);
   }
}

Output:

6
true
0.2805854768046321
0.13993335
0.32107779397797304
[-114 29 112 38 -67 -67 81 -58 -18 51 ]
9211389898189124398
-1318125037

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

LohithS

1 bài viết.
4 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
1 bài viết.
4 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!