Java. Class Random. Methods for obtaining sets of random numbers

Class Random. Methods for obtaining sets of random numbers


Contents


Search on other resources:

1. Method doubles(). Create a data stream containing random numbers of double type

The doubles() method creates a stream of data consisting of random numbers of the double type. The numbers have a value between 0 and 1. The method has several implementations. The most common method implementations are as follows

DoubleStream doubles();
DoubleStream doubles(long streamSize);

where

  • streamSize – number of numbers to be generated in the data stream.

The first implementation (without parameters) allows you to create an unlimited data stream. The second implementation of the doubles() method generates a fixed size data stream.

To use the DoubleStream class, you need to include a module.

import java.util.stream.DoubleStream;

 

Example.

import java.util.Iterator; // needed to use the Iterator<> class
import java.util.Random;   // needed to use the Random class
import java.util.stream.DoubleStream; // needed to use the DoubleStream class

public class MathFunctions {

  public static void main(String[] args) {

    // 1. Create a stream based on the arbitrary initial value
    Random r1 = new Random();

    // 2. Get a stream of 10 double numbers
    DoubleStream ds1 = r1.doubles(10);

    // 3. Get iterator on stream ds1
    Iterator<Double> it1 = ds1.iterator();

    // 4. Output in a loop 10 random numbers from the stream ds1
    System.out.println("Stream ds1:");

    while (it1.hasNext()) {
      System.out.println(it1.next());
    }
  }
}

Result

Stream ds1:
0.03296957043047
0.5816373543355657
0.6067884839512304
0.3600735235508934
0.5041601996294538
0.8372554459494158
0.7894095095554403
0.23663204256820447
0.5334022228843057
0.5142652199413285

 

2. Method ints(). Get data of type int as a stream of random numbers

The ints() method creates a stream of random numbers of type IntStream. The most common method implementations are as follows

IntStream ints();
IntStream ints(long streamSize);

where

  • streamSize – number of elements in the data stream of type int.

The first implementation of the method (without parameters) creates a stream of random numbers of type int of unlimited size. The second implementation allows you to set a streamSize limit on the stream.

To use the IntStream type and similar types, you need to include the line at the beginning of the module

import java.util.stream.*;

 

Example.

In the example, the ints() method with no parameters creates a stream of unlimited size. Then the first 5 elements of the stream are displayed.

import java.util.Iterator; // needed to use the Iterator<> class
import java.util.Random;   // needed to use Random class
import java.util.stream.*; // needed to use the IntStream class

public class MathFunctions {

  public static void main(String[] args) {

    // 1. Create a stream based on an arbitrary initial value
    Random r1 = new Random();

    // 2. Get a stream with an unlimited number of int numbers
    IntStream rndInt = r1.ints();

    // 3. Get iterator on rndInt stream
    Iterator<Integer> it1 = rndInt.iterator();

    // 4. Output 5 random numbers from the rndInt stream in the loop
    int value;

    System.out.println("Stream rndInt:");

    for (int i=0; i<5; i++) {
      // Get a random number
      value = it1.next();

      // Display the number on the screen
      System.out.println(value);
    }
  }
}

Result

Stream rndInt:
2119189406
-15495572
-2068391741
271674312
742325160

 

3. Method longs(). Get data of type long as a stream of random numbers

The longs() method is similar to the doubles() and ints() methods. The method generates a stream of random numbers of the LongStream type. The declaration of the most common method implementations is

LongStream longs();
LongStream longs(long streamSize);

where

  • streamSize – the number of long numbers to be formed.

The first implementation of the method generates a stream of numbers of unlimited size. The second implementation generates a stream of 10 numbers.

 

Example.

The example initially generates a stream of 10 random long numbers. This stream is then displayed on the screen.

import java.util.Iterator; // needed to use the Iterator<> class
import java.util.Random;   // needed to use the Random class
import java.util.stream.*; // needed to use the LongStream class

public class MathFunctions {

  public static void main(String[] args) {

    // 1. Create a stream based on an arbitrary initial value
    Random rnd = new Random();

    // 2. Get a stream containing 10 long numbers - the longs() method
    LongStream stream = rnd.longs(10);

    // 3. Get an iterator on the stream
    Iterator<Long> it = stream.iterator();

    // 4. Output in a loop 10 random numbers from the stream
    System.out.println("Stream of Long numbers:");

    for (int i=0; i<10; i++) {
      // Get a random number of type long
      long value = it.next();

      // Display the number on the screen
      System.out.println(value);
    }
  }
}

Result

Stream of Long numbers:
-846271452660413200
-3239665480282540853
-4825523866103987302
-6364844712541754488
752243074034471911
6039172197582870129
9130557788351257634
5320620246218395762
-5038665701090253375
-924371785716259808

 

4. Method nextBytes(). Get an array of random numbers of type byte[]

The nextBytes() method fills the input array with random numbers of type byte. According to the documentation, the method declaration syntax is as follows

void nextBytes(byte[] bytes);

where

  • bytes – given array of some length. All elements of the array are filled. Array element values are within [-128; 127].

 

Example.

import java.util.Random;

public class MathFunctions {

  public static void main(String[] args) {

    // 1. Create a stream based on an arbitrary initial value
    Random rnd = new Random();

    // 2. Create array AB with 10 byte elements
    byte[] AB = new byte[10];

    // 3. Fill array AB with random numbers
    rnd.nextBytes(AB);

    // 4. Display array on the screen
    System.out.println("Array AB:");

    for (int i=0; i<AB.length; i++)
      System.out.println(AB[i]+" ");
  }
}

Result

Array AB:
-67
-86
98
-61
16
-44
-73
96
-40
-80

 


Related topics