RandomNumberGenerator

package
v0.0.0-...-357ca8a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2025 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

RandomNumberGenerator is a class for generating pseudo-random numbers. It currently uses PCG32.

Note: The underlying algorithm is an implementation detail and should not be depended upon.

To generate a random float number (within a given range) based on a time-dependent seed:

package main

import "graphics.gd/classdb/RandomNumberGenerator"

func ExampleRandomNumberGenerator() {
	var rng = RandomNumberGenerator.New()
	var my_random_number = rng.RandfRange(-10.0, 10.0)
	_ = my_random_number
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Advanced

type Advanced = class

Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.

type Any

type Any interface {
	gd.IsClass
	AsRandomNumberGenerator() Instance
}

type Expanded

type Expanded = MoreArgs

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsRandomNumberGenerator

func (self *Extension[T]) AsRandomNumberGenerator() Instance

func (*Extension[T]) AsRefCounted

func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted

type ID

type ID Object.ID

ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Instance

type Instance [1]gdclass.RandomNumberGenerator

Instance of the class with convieniently typed arguments and results.

var Nil Instance

Nil is a nil/null instance of the class. Equivalent to the zero value.

func New

func New() Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsRandomNumberGenerator

func (self Instance) AsRandomNumberGenerator() Instance

func (Instance) AsRefCounted

func (self Instance) AsRefCounted() [1]gd.RefCounted

func (Instance) ID

func (self Instance) ID() ID

func (Instance) MoreArgs

func (self Instance) MoreArgs() MoreArgs

MoreArgs enables certain functions to be called with additional 'optional' arguments.

func (Instance) RandWeighted

func (self Instance) RandWeighted(weights []float32) int

Returns a random index with non-uniform weights. Prints an error and returns -1 if the array is empty.

func (Instance) Randf

func (self Instance) Randf() Float.X

Returns a pseudo-random float between 0.0 and 1.0 (inclusive).

func (Instance) RandfRange

func (self Instance) RandfRange(from Float.X, to Float.X) Float.X

Returns a pseudo-random float between 'from' and 'to' (inclusive).

func (Instance) Randfn

func (self Instance) Randfn() Float.X

Returns a normally-distributed, pseudo-random floating-point number from the specified 'mean' and a standard 'deviation'. This is also known as a Gaussian distribution.

Note: This method uses the Box-Muller transform algorithm.

func (Instance) Randi

func (self Instance) Randi() int

Returns a pseudo-random 32-bit unsigned integer between 0 and 4294967295 (inclusive).

func (Instance) RandiRange

func (self Instance) RandiRange(from int, to int) int

Returns a pseudo-random 32-bit signed integer between 'from' and 'to' (inclusive).

func (Instance) Randomize

func (self Instance) Randomize()

Sets up a time-based seed for this RandomNumberGenerator instance. Unlike the standard random number generation functions, different RandomNumberGenerator instances can use different seeds.

func (Instance) Seed

func (self Instance) Seed() int

Initializes the random number generator state based on the given seed value. A given seed will give a reproducible sequence of pseudo-random numbers.

Note: The RNG does not have an avalanche effect, and can output similar random streams given similar seeds. Consider using a hash function to improve your seed quality if they're sourced externally.

Note: Setting this property produces a side effect of changing the internal State, so make sure to initialize the seed before modifying the State:

Note: The default value of this property is pseudo-random, and changes when calling Randomize. The 0 value documented here is a placeholder, and not the actual default seed.

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) SetSeed

func (self Instance) SetSeed(value int)

SetSeed sets the property returned by [GetSeed].

func (Instance) SetState

func (self Instance) SetState(value int)

SetState sets the property returned by [GetState].

func (Instance) State

func (self Instance) State() int

The current state of the random number generator. Save and restore this property to restore the generator to a previous state:

Note: Do not set state to arbitrary values, since the random number generator requires the state to have certain qualities to behave properly. It should only be set to values that came from the state property itself. To initialize the random number generator with arbitrary input, use Seed instead.

Note: The default value of this property is pseudo-random, and changes when calling Randomize. The 0 value documented here is a placeholder, and not the actual default state.

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

type MoreArgs

type MoreArgs [1]gdclass.RandomNumberGenerator

MoreArgs is a container for Instance functions with additional 'optional' arguments.

func (MoreArgs) Randfn

func (self MoreArgs) Randfn(mean Float.X, deviation Float.X) Float.X

Returns a normally-distributed, pseudo-random floating-point number from the specified 'mean' and a standard 'deviation'. This is also known as a Gaussian distribution.

Note: This method uses the Box-Muller transform algorithm.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL