Class

class Random

public open class Random

Description: Provides functions related to the generation of pseudo-random numbers.

Example:

import std.random.*
main() {
    let m: Random = Random()
    /* Creates a Random object and sets a seed for obtaining the random object. */
    m.seed = 3
    let b: Bool = m.nextBool()
    let c: Int8 = m.nextInt8()
    print("b=${b is Bool},")/* The object can also be of the Bool type. */
    println("c=${c is Int8}")
    return 0
}

Running result:

b=true,c=true

prop seed

public open mut prop seed: UInt64

Description: Sets or obtains the size of a seed. If same random seeds are set, the generated pseudo-random number lists are the same.

Type: UInt64

init()

public init()

Description: Creates a new Random object using a non-parameterized constructor by default.

init(UInt64)

public init(seed: UInt64)

Description: Creates a new Random object using a random seed.

Parameters:

  • seed:UInt64: random seed. If same random seeds are set, the generated pseudo-random number lists are the same.

func next(UInt64)

public open func next(bits: UInt64): UInt64

Description: Generates a random integer with a user-specified length.

Parameters:

  • bits: UInt64: number of bits of the pseudo-random number to be generated. The value range is (0, 64].

Returns:

  • UInt64: pseudo-random number with a specified length

Throws:

func nextBool()

public open func nextBool(): Bool

Description: Obtains a pseudo-random value of the Bool type.

Returns:

  • Bool: pseudo-random number of the Bool type

func nextFloat16()

public open func nextFloat16(): Float16

Description: Obtains a pseudo-random number of the Float16 type. The range is [0.0, 1.0).

Returns:

func nextFloat32()

public open func nextFloat32(): Float32

Description: Obtains a pseudo-random number of the Float32 type. The range is [0.0, 1.0).

Returns:

func nextFloat64()

public open func nextFloat64(): Float64

Description: Obtains a pseudo-random number of the Float64 type. The range is [0.0, 1.0).

Returns:

func nextGaussianFloat16(Float16, Float16)

public func nextGaussianFloat16(mean!: Float16 = 0.0, sigma!: Float16 = 1.0): Float16

Description: Obtains a random number of the Float16 type complying with a Gaussian distribution where the mean and standard deviation are specified.

By default, a random number of the Float16 type is obtained, complying with the Gaussian distribution where the mean is 0.0 and the standard deviation is 1.0. The mean is an expected value and can be interpreted as a location parameter, which determines the location of the distribution. The standard deviation can be interpreted as a scale parameter, which determines the amplitude of the distribution. This function calls the function nextGaussianFloat16Implement to obtain a return value. Therefore, when a subclass inherits Random and overwrites the function nextGaussianFloat64Implement, the return value of the overwritten function is returned when this function of the subclass is called.

Parameters:

  • mean!: Float16: mean; default value: 0.0
  • sigma!: Float16: standard deviation; default value: 1.0

Returns:

func nextGaussianFloat16Implement(Float16, Float16)

protected open func nextGaussianFloat16Implement(mean: Float16, sigma: Float16): Float16

Description: Specifies an implementation function of nextGaussianFloat16, used to obtain a random number of the Float16 type complying with a Gaussian distribution where the mean and standard deviation are specified.

A random number of the Float16 type is obtained, complying with a Gaussian distribution where the mean is mean and the standard deviation is sigma. The mean is an expected value and can be interpreted as a location parameter, which determines the location of the distribution. The standard deviation can be interpreted as a scale parameter, which determines the amplitude of the distribution. This function is the implementation function of nextGaussianFloat16 and is not public. When a subclass inherits Random and overwrites this function, the return value of this function is returned when the function nextGaussianFloat16 of the subclass is called.

Parameters:

Returns:

func nextGaussianFloat32(Float32, Float32)

public func nextGaussianFloat32(mean!: Float32 = 0.0, sigma!: Float32 = 1.0): Float32

Description: Obtains a random number of the Float32 type complying with a Gaussian distribution where the mean and standard deviation are specified.

By default, a random number of the Float32 type is obtained, complying with a Gaussian distribution where the mean is 0.0 and the standard deviation is 1.0. The mean is an expected value and can be interpreted as a location parameter, which determines the location of the distribution. The standard deviation can be interpreted as a scale parameter, which determines the amplitude of the distribution. This function calls the function nextGaussianFloat32Implement to obtain a return value. Therefore, when a subclass inherits Random and overwrites the function nextGaussianFloat64Implement, the return value of the overwritten function is returned when this function of the subclass is called.

Parameters:

  • mean!: Float32: mean; default value: 0.0
  • sigma!: Float32: standard deviation; default value: 1.0

Returns:

func nextGaussianFloat32Implement(Float32, Float32)

protected open func nextGaussianFloat32Implement(mean: Float32, sigma: Float32): Float32

Description: Specifies an implementation function of nextGaussianFloat32, used to obtain a random number of the Float32 type complying with a Gaussian distribution where the mean and standard deviation are specified.

A random number of the Float32 type is obtained, complying with a Gaussian distribution where the mean is mean and the standard deviation is sigma. The mean is an expected value and can be interpreted as a location parameter, which determines the location of the distribution. The standard deviation can be interpreted as a scale parameter, which determines the amplitude of the distribution. This function is the implementation function of nextGaussianFloat32 and is not public. When a subclass inherits Random and overwrites this function, the return value of this function is returned when the function nextGaussianFloat32 of the subclass is called.

Parameters:

Returns:

func nextGaussianFloat64(Float64, Float64)

public func nextGaussianFloat64(mean!: Float64 = 0.0, sigma!: Float64 = 1.0): Float64

Description: Obtains a random number of the Float64 type complying with a Gaussian distribution where the mean and standard deviation are specified.

By default, a random number of the Float64 type is obtained, complying with a Gaussian distribution where the mean is 0.0 and the standard deviation is 1.0. The mean is an expected value and can be interpreted as a location parameter, which determines the location of the distribution. The standard deviation can be interpreted as a scale parameter, which determines the amplitude of the distribution. This function calls the function nextGaussianFloat64Implement to obtain a return value. Therefore, when a subclass inherits Random and overwrites the function nextGaussianFloat64Implement, the return value of the overwritten function is returned when this function of the subclass is called.

Parameters:

  • mean!: Float64: mean; default value: 0.0
  • sigma!: Float64: standard deviation; default value: 1.0

Returns:

func nextGaussianFloat64Implement(Float64, Float64)

protected open func nextGaussianFloat64Implement(mean: Float64, sigma: Float64): Float64

Description: Specifies an implementation function of nextGaussianFloat64, used to obtain a random number of the Float64 type complying with a Gaussian distribution where the mean and standard deviation are specified.

A random number of the Float64 type is obtained, complying with a Gaussian distribution where the mean is mean and the standard deviation is sigma. The mean is an expected value and can be interpreted as a location parameter, which determines the location of the distribution. The standard deviation can be interpreted as a scale parameter, which determines the amplitude of the distribution. This function is the implementation function of nextGaussianFloat64 and is not public. When a subclass inherits Random and overwrites this function, the return value of this function is returned when the function nextGaussianFloat64 of the subclass is called.

Parameters:

Returns:

func nextInt16()

public open func nextInt16(): Int16

Description: Obtains a pseudo-random number of the Int16 type.

Returns:

func nextInt16(Int16)

public open func nextInt16(upper: Int16): Int16

Description: Obtains a pseudo-random number of the Int16 type with the range of [0, upper).

Parameters:

  • upper: Int16: upper bound of the generated pseudo-random number (not including upper). The value range is (0, Int16.Max].

Returns:

Throws:

func nextInt32()

public open func nextInt32(): Int32

Description: Obtains a pseudo-random number of the Int32 type.

Returns:

func nextInt32(Int32)

public open func nextInt32(upper: Int32): Int32

Description: Obtains a pseudo-random number of the Int32 type with the range of [0, upper).

Parameters:

  • upper: Int32: upper bound of the generated pseudo-random number (not including upper). The value range is (0, Int32.Max].

Returns:

Throws:

func nextInt64()

public open func nextInt64(): Int64

Description: Obtains a pseudo-random number of the Int64 type.

Returns:

func nextInt64(Int64)

public open func nextInt64(upper: Int64): Int64

Description: Obtains a pseudo-random number of the Int64 type with the range of [0, upper).

Parameters:

  • upper: Int64: upper bound of the generated pseudo-random number (not including upper). The value range is (0, Int64.Max].

Returns:

Throws:

func nextInt8()

public open func nextInt8(): Int8

Description: Obtains a pseudo-random number of the Int8 type.

Returns:

  • Int8: pseudo-random number of the Int8 type

func nextInt8(Int8): Int8

public open func nextInt8(upper: Int8): Int8

Description: Obtains a pseudo-random number of the Int8 type with the range of [0, upper).

Parameters:

  • upper: Int8: upper bound of the generated pseudo-random number (not including upper). The value range is (0, Int8.Max].

Returns:

  • Int8: pseudo-random number of the Int8 type

Throws:

func nextUInt16()

public open func nextUInt16(): UInt16

Description: Obtains a pseudo-random number of the UInt16 type.

Returns:

func nextUInt16(UInt16)

public open func nextUInt16(upper: UInt16): UInt16

Description: Obtains a pseudo-random number of the UInt16 type with the range of [0, upper).

Parameters:

  • upper: UInt16: upper bound of the generated pseudo-random number (not including upper). The value range is (0, UInt16.Max].

Returns:

Throws:

func nextUInt32()

public open func nextUInt32(): UInt32

Description: Obtains a pseudo-random number of the UInt32 type.

Returns:

func nextUInt32(UInt32)

public open func nextUInt32(upper: UInt32): UInt32

Description: Obtains a pseudo-random number of the UInt32 type with the range of [0, upper).

Parameters:

  • upper: UInt32: upper bound of the generated pseudo-random number (not including upper). The value range is (0, UInt32.Max].

Returns:

Throws:

func nextUInt64()

public open func nextUInt64(): UInt64

Description: Obtains a pseudo-random number of the UInt64 type.

Returns:

func nextUInt64(UInt64)

public open func nextUInt64(upper: UInt64): UInt64

Description: Obtains a pseudo-random number of the UInt64 type with the range of [0, upper).

Parameters:

  • upper: UInt64: upper bound of the generated pseudo-random number (not including upper). The value range is (0, UInt64.Max].

Returns:

Throws:

func nextUInt8()

public open func nextUInt8(): UInt8

Description: Obtains a pseudo-random number of the UInt8 type.

Returns:

func nextUInt8(UInt8)

public open func nextUInt8(upper: UInt8): UInt8

Description: Obtains a pseudo-random number of the UInt8 type with the range of [0, upper).

Parameters:

  • upper: UInt8: upper bound of the generated pseudo-random number (not including upper). The value range is (0, UInt8.Max].

Returns:

Throws:

func nextUInt8s(Array<UInt8>)

public open func nextUInt8s(array: Array<UInt8>): Array<UInt8>

Description: Generates a random number to replace each element in an input parameter array.

Parameters:

Returns: