Class
class Random
public class Random {
public init()
public init(seed: UInt64)
}
Description: Provides functionalities related to the generation of pseudo-random numbers.
Examples:
import std.random.*
main() {
/* Create a Random object and set the seed to obtain the Random object. */
let m: Random = Random(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
}
Results:
b=true,c=true
prop seed
public prop seed: UInt64
Description: Obtains the random number seed.
Type: UInt64
init()
public init()
Description: Creates a random object using the default parameterless constructor.
init(UInt64)
public init(seed: UInt64)
Description: Creates a random object using the random number seed.
Parameters:
- seed: UInt64: random number seed. If the same random seed is set, the same pseudo-random number list is generated.
func next(UInt64) (deprecated)
public func next(bits: UInt64): UInt64
Description: Generates a random integer with a user-defined bit length.
NOTE
This function will be deprecated in future releases and nextBits will be used instead.
Parameters:
- bits: UInt64: bit length of the pseudo-random number to be generated. The value range is (0,64].
Returns:
- UInt64: pseudo-random number with the user-defined bit length
Throws:
- IllegalArgumentException: If the value of
bitsis equal to 0 or greater than 64, exceeding the maximum length of UInt64, this exception is thrown.
func nextBits(UInt64)
public func nextBits(bits: UInt64): UInt64
Description: Generates a random integer with a user-defined bit length.
Parameters:
- bits: UInt64: bit length of the pseudo-random number to be generated. The value range is (0,64].
Returns:
- UInt64: generated pseudo-random number with the user-defined bit length
Throws:
- IllegalArgumentException: If the value of
bitsis equal to 0 or greater than 64, exceeding the maximum length of UInt64, this exception is thrown.
func nextBool()
public func nextBool(): Bool
Description: Obtains a pseudo-random value of the Bool type.
Returns:
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: Bool = m.nextBool()
println("n=${n is Bool}")
return 0
}
Results:
n=true
func nextFloat16()
public func nextFloat16(): Float16
Description: Obtains a pseudo-random number of the Float16 type, whose value range is [0.0,1.0).
Returns:
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: Float16 = m.nextFloat16()
if (n is Float16) {
println("n is Float16")
}
return 0
}
Results:
n is Float16
func nextFloat32()
public func nextFloat32(): Float32
Description: Obtains a pseudo-random number of the Float32 type, whose value range is [0.0,1.0).
Returns:
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: Float32 = m.nextFloat32()
if (n is Float32) {
println("n is Float32")
}
return 0
}
Results:
n is Float32
func nextFloat64()
public func nextFloat64(): Float64
Description: Obtains a pseudo-random number of the Float64 type, whose value range is [0.0,1.0).
Returns:
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: Float64 = m.nextFloat64()
if (n is Float64) {
println("n is Float64")
}
return 0
}
Results:
n is Float64
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, following a Gaussian distribution with the specified mean and standard deviation.
By default, a random number of the Float16 type is obtained, following a Gaussian distribution with a mean of 0.0 and a standard deviation of 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.
Parameters:
- mean!: Float16: mean. The default value is 0.0.
- sigma!: Float16: standard deviation. The default value is 1.0.
Returns:
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: Float16 = m.nextGaussianFloat16(mean: 0.0, sigma: 1.0)
if (n is Float16) {
println("n is Float16")
}
return 0
}
Results:
n is Float16
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, following a Gaussian distribution with the specified mean and standard deviation.
By default, a random number of the Float32 type is obtained, following a Gaussian distribution with a mean of 0.0 and a standard deviation of 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.
Parameters:
- mean!: Float32: mean. The default value is 0.0.
- sigma!: Float32: standard deviation. The default value is 1.0.
Returns:
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: Float32 = m.nextGaussianFloat32(mean: 0.0, sigma: 1.0)
if (n is Float32) {
println("n is Float32")
}
return 0
}
Results:
n is Float32
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, following a Gaussian distribution with the specified mean and standard deviation.
By default, a random number of the Float64 type is obtained, following a Gaussian distribution with a mean of 0.0 and a standard deviation of 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.
Parameters:
- mean!: Float64: mean. The default value is 0.0.
- sigma!: Float64: standard deviation. The default value is 1.0.
Returns:
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: Float64 = m.nextGaussianFloat64(mean: 0.0, sigma: 1.0)
if (n is Float64) {
println("n is Float64")
}
return 0
}
Results:
n is Float64
func nextInt16()
public func nextInt16(): Int16
Description: Obtains a pseudo-random number of the Int16 type.
Returns:
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: Int16 = m.nextInt16()
if (n is Int16) {
println("n is Int16")
}
return 0
}
Results:
n is Int16
func nextInt16(Int16)
public func nextInt16(upper: Int16): Int16
Description: Obtains a pseudo-random number of the Int16 type, whose value range is [0,upper).
Parameters:
- upper: Int16: Upper bound (excluding
upper) of the generated pseudo-random number. The value range is (0,Int16.Max].
Returns:
Throws:
- IllegalArgumentException: If the value of
upperis less than or equal to 0, this exception is thrown.
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: Int16 = m.nextInt16(5)
if (n is Int16) {
println("n is Int16")
}
try {
let p: Int16 = m.nextInt16(-1)
println(p)
} catch (e: IllegalArgumentException) {
println("Parameter Throws: upper is less than or equal to 0")
}
return 0
}
Results:
n is Int16
Parameter Throws: upper is less than or equal to 0
func nextInt32()
public func nextInt32(): Int32
Description: Obtains a pseudo-random number of the Int32 type.
Returns:
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: Int32 = m.nextInt32()
if (n is Int32) {
println("n is Int32")
}
return 0
}
Results:
n is Int32
func nextInt32(Int32)
public func nextInt32(upper: Int32): Int32
Description: Obtains a pseudo-random number of the Int32 type, whose value range is [0,upper).
Parameters:
- upper: Int32: Upper bound (excluding
upper) of the generated pseudo-random number. The value range is (0,Int32.Max].
Returns:
Throws:
- IllegalArgumentException: If the value of
upperis less than or equal to 0, this exception is thrown.
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: Int32 = m.nextInt32(5)
if (n is Int32) {
println("n is Int32")
}
try {
let p: Int32 = m.nextInt32(-1)
println(p)
} catch (e: IllegalArgumentException) {
println("Parameter Throws: upper is less than or equal to 0")
}
return 0
}
Results:
n is Int32
Parameter Throws: upper is less than or equal to 0
func nextInt64()
public func nextInt64(): Int64
Description: Obtains a pseudo-random number of the Int64 type.
Returns:
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: Int64 = m.nextInt64()
if (n is Int64) {
println("n is Int64")
}
return 0
}
Results:
n is Int64
func nextInt64(Int64)
public func nextInt64(upper: Int64): Int64
Description: Obtains a pseudo-random number of the Int64 type, whose value range is [0,upper).
Parameters:
- upper: Int64: Upper bound (excluding
upper) of the generated pseudo-random number. The value range is (0,Int64.Max].
Returns:
Throws:
- IllegalArgumentException: If the value of
upperis less than or equal to 0, this exception is thrown.
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: Int64 = m.nextInt64(5)
if (n is Int64) {
println("n is Int64")
}
try {
let p: Int64 = m.nextInt64(-1)
println(p)
} catch (e: IllegalArgumentException) {
println("Parameter Throws: upper is less than or equal to 0")
}
return 0
}
Results:
n is Int64
Parameter Throws: upper is less than or equal to 0
func nextInt8()
public func nextInt8(): Int8
Description: Obtains a pseudo-random number of the Int8 type.
Returns:
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: Int8 = m.nextInt8()
if (n is Int8) {
println("n is Int8")
}
return 0
}
Results:
n is Int8
func nextInt8(Int8): Int8
public func nextInt8(upper: Int8): Int8
Description: Obtains a pseudo-random number of the Int8 type, whose value range is [0,upper).
Parameters:
- upper: Int8: Upper bound (excluding
upper) of the generated pseudo-random number. The value range is (0,Int8.Max].
Returns:
Throws:
- IllegalArgumentException: If the value of
upperis less than or equal to 0, this exception is thrown.
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: Int8 = m.nextInt8(5)
if (n is Int8) {
println("n is Int8")
}
try {
let p: Int8 = m.nextInt8(-1)
println(p)
} catch (e: IllegalArgumentException) {
println("Parameter Throws: upper is less than or equal to 0")
}
return 0
}
Results:
n is Int8
Parameter Throws: upper is less than or equal to 0
func nextUInt16()
public func nextUInt16(): UInt16
Description: Obtains a pseudo-random number of the UInt16 type.
Returns:
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: UInt16 = m.nextUInt16()
if (n is UInt16) {
println("n is UInt16")
}
return 0
}
Results:
n is UInt16
func nextUInt16(UInt16)
public func nextUInt16(upper: UInt16): UInt16
Description: Obtains a pseudo-random number of the UInt16 type, whose value range is [0,upper).
Parameters:
- upper: UInt16: Upper bound (excluding
upper) of the generated pseudo-random number. The value range is (0,UInt16.Max].
Returns:
Throws:
- IllegalArgumentException: If the value of
upperis equal to 0, this exception is thrown.
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: UInt16 = m.nextUInt16(5)
if (n is UInt16) {
println("n is UInt16")
}
try {
let p: UInt16 = m.nextUInt16(0)
println(p)
} catch (e: IllegalArgumentException) {
println("Parameter Throws: upper is equal to 0")
}
return 0
}
Results:
n is UInt16
Parameter Throws: upper is equal to 0
func nextUInt32()
public func nextUInt32(): UInt32
Description: Obtains a pseudo-random number of the UInt32 type.
Returns:
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: UInt32 = m.nextUInt32()
if (n is UInt32) {
println("n is UInt32")
}
return 0
}
Results:
n is UInt32
func nextUInt32(UInt32)
public func nextUInt32(upper: UInt32): UInt32
Description: Obtains a pseudo-random number of the UInt32 type, whose value range is [0,upper).
Parameters:
- upper: UInt32: Upper bound (excluding
upper) of the generated pseudo-random number. The value range is (0,UInt32.Max].
Returns:
Throws:
- IllegalArgumentException: If the value of
upperis equal to 0, this exception is thrown.
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: UInt32 = m.nextUInt32(5)
if (n is UInt32) {
println("n is UInt32")
}
try {
let p: UInt32 = m.nextUInt32(0)
println(p)
} catch (e: IllegalArgumentException) {
println("Parameter Throws: upper is equal to 0")
}
return 0
}
Results:
n is UInt32
Parameter Throws: upper is equal to 0
func nextUInt64()
public func nextUInt64(): UInt64
Description: Obtains a pseudo-random number of the UInt64 type.
Returns:
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: UInt64 = m.nextUInt64()
if (n is UInt64) {
println("n is UInt64")
}
return 0
}
Results:
n is UInt64
func nextUInt64(UInt64)
public func nextUInt64(upper: UInt64): UInt64
Description: Obtains a pseudo-random number of the UInt64 type, whose value range is [0,upper).
Parameters:
- upper: UInt64: Upper bound (excluding
upper) of the generated pseudo-random number. The value range is (0,UInt64.Max].
Returns:
Throws:
- IllegalArgumentException: If the value of
upperis equal to 0, this exception is thrown.
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: UInt64 = m.nextUInt64(5)
if (n is UInt64) {
println("n is UInt64")
}
try {
let p: UInt64 = m.nextUInt64(0)
println(p)
} catch (e: IllegalArgumentException) {
println("Parameter Throws: upper is equal to 0")
}
return 0
}
Results:
n is UInt64
Parameter Throws: upper is equal to 0
func nextUInt8()
public func nextUInt8(): UInt8
Description: Obtains a pseudo-random number of the UInt8 type.
Returns:
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: UInt8 = m.nextUInt8()
if (n is UInt8) {
println("n is UInt8")
}
return 0
}
Results:
n is UInt8
func nextUInt8(UInt8)
public func nextUInt8(upper: UInt8): UInt8
Description: Obtains a pseudo-random number of the UInt8 type, whose value range is [0,upper).
Parameters:
- upper: UInt8: Upper bound (excluding
upper) of the generated pseudo-random number. The value range is (0,UInt8.Max].
Returns:
Throws:
- IllegalArgumentException: If the value of
upperis equal to 0, this exception is thrown.
Examples:
import std.random.*
main() {
let m: Random = Random()
let n: UInt8 = m.nextUInt8(5)
if (n is UInt8) {
println("n is UInt8")
}
try {
let p: UInt8 = m.nextUInt8(0)
println(p)
} catch (e: IllegalArgumentException) {
println("Parameter Throws: upper is equal to 0")
}
return 0
}
Results:
n is UInt8
Parameter Throws: upper is equal to 0
func nextUInt8s(Array<UInt8>) (deprecated)
public func nextUInt8s(array: Array<UInt8>): Array<UInt8>
Description: Generates a random number to replace each element in an input parameter array.
NOTE
This function will be deprecated in future releases and nextBytes will be used instead.
Parameters:
Returns:
func nextBytes(Array<Byte>)
public func nextBytes(bytes: Array<Byte>): Unit
Description: Generates a random number to replace each element in an input parameter array.
Parameters:
func nextBytes(Int32)
public func nextBytes(length: Int32): Array<Byte>
Description: Generates a random number array with a specified length.
Parameters:
- length: Int32: length of the generated random number array. The value of
lengthmust be greater than 0.
Returns:
Throws:
- IllegalArgumentException: If the value of
lengthis less than or equal to 0, this exception is thrown.