Aego Русский

aego/rand

Int(min, max) and Range(min, max) include min and exclude max. Every script of a world draws from the same generator, so the numbers a script gets depend on the order of calls in the frame. The documentation sandbox always runs with seed 1.

On this page

Example

package demo

import (
    "aego/log"
    "aego/rand"
)

script Loot {
    @export var DropChance float = 0.5

    func Ready() {
        log.Info("seed " + string(rand.Seed()))
        for i := 0; i < 5; i++ {
            roll := rand.Int(1, 7)
            if rand.Float() < DropChance {
                gold := rand.Int(5, 20)
                log.Info("chest " + string(i) + ": rolled " + string(roll) + ", drops " + string(gold) + " gold")
            } else {
                log.Info("chest " + string(i) + ": rolled " + string(roll) + ", empty")
            }
        }
        delay := rand.Range(0.5, 1.5)
        log.Info("next spawn in " + string(delay) + " s, facing left: " + string(rand.Bool()))
    }
}

Functions

Bool() boolReturns true or false with equal chance.
Float() floatReturns a random number in [0, 1).
Int(min int, max int) intReturns a random int in [min, max). It returns min when max <= min.
Range(min float, max float) floatReturns a random float in [min, max).
Seed() intReturns the seed of the world's generator. Replays with the same seed produce the same numbers.

Bool function

func Bool() bool

Returns true or false with equal chance.

Example

flip := rand.Bool()

Float function

func Float() float

Returns a random number in [0, 1).

Example

if rand.Float() < 0.25 {
	dropLoot()
}

Int function

func Int(min int, max int) int

Returns a random int in [min, max). It returns min when max <= min.

Example

roll := rand.Int(1, 7)

Range function

func Range(min float, max float) float

Returns a random float in [min, max).

Example

delay := rand.Range(0.5, 1.5)

Seed function

func Seed() int

Returns the seed of the world's generator. Replays with the same seed produce the same numbers.

Example

log.Info("seed " + string(rand.Seed()))