RandomNumberGenerator
Provides methods for generating pseudo-random numbers.
RandomNumberGenerator.swift:22class RandomNumberGenerator
RandomNumberGenerator is a class for generating pseudo-random numbers. It currently uses PCG32.
To generate a random float number (within a given range) based on a time-dependent seed:
Superclasses
class RefCounted
Base class for reference-counted objects.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Type members
Instance members
var seed: UInt
Initializes the random number generator state based on the given seed value. A given seed will give a reproducible sequence of pseudo-random numbers.
var state: UInt
The current state of the random number generator. Save and restore this property to restore the generator to a previous state:
func randWeighted(weights: PackedFloat32Array
) -> Int Returns a random index with non-uniform weights. Prints an error and returns
-1
if the array is empty.func randf(
) -> Double Returns a pseudo-random float between
0.0
and1.0
(inclusive).func randfRange(from: Double, to: Double
) -> Double Returns a pseudo-random float between
from
andto
(inclusive).func randfn(mean: Double, deviation: Double
) -> Double Returns a normally-distributed, pseudo-random floating-point number from the specified
mean
and a standarddeviation
. This is also known as a Gaussian distribution.func randi(
) -> UInt32 Returns a pseudo-random 32-bit unsigned integer between
0
and4294967295
(inclusive).func randiRange(from: Int32, to: Int32
) -> Int32 Returns a pseudo-random 32-bit signed integer between
from
andto
(inclusive).func randomize(
) Sets up a time-based seed for this
RandomNumberGenerator
instance. Unlike the [@GlobalScope] random number generation functions, differentRandomNumberGenerator
instances can use different seeds.