> For the complete documentation index, see [llms.txt](https://docs.allout.game/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.allout.game/scripting/random-math-and-more.md).

# Random, Math, & More

This page is a grab-bag of “you’ll use these constantly” utilities: RNG, formatting, time, and a few core subsystems that show up in most games.

{% hint style="info" %}
If you’re looking for arrays/slices, see [Arrays and Collections](/scripting/arrays-and-collections.md).
{% endhint %}

## Random numbers (RNG)

Random uses an explicit `u64` seed. Pass the seed by `ref` so it updates.

```go
rng: u64 = rng_root_seed();
// or deterministic per-entity:
// rng: u64 = rng_seed(entity.id);

// Range values are inclusive
roll := rng_range_int(ref rng, 1, 10);
chance := rng_range_float(ref rng, 0, 1);

// Random points and shuffling
offset := rng_disk(ref rng, 0.5, 2.0);
spawn_points: [..]v2;
spawn_points.append({0, 0});
spawn_points.append({4, 0});
rng_shuffle(ref rng, spawn_points);
```

{% hint style="warning" %}
If you want deterministic behavior for gameplay, keep RNG server-authoritative. Don’t roll important outcomes only on the client.
{% endhint %}

## Math functions

Common math helpers:

```go
angle_sin := sin(x);
angle_cos := cos(x);
angle := atan2(dir.y, dir.x);
degrees := to_degrees(angle);
radians := to_radians(degrees);

result := pow(2.0, 3.0);   // 8.0
root := sqrt(16.0);        // 4.0

value := lerp(0.0, 100.0, 0.5);  // 50.0
clamped := clamp(value, 0.0, 10.0);
stepped := linear_step(0.0, 1.0, value);

absolute := abs(-5);
minimum := min(5, 10);
maximum := max(5, 10);
direction_sign := sign(-12);

len := length(v);
len_sq := length_squared(v);
normalized := normalize(v);
facing := dot(normalized, {1, 0});
```

## String formatting

Use `%` placeholders with an argument array:

```go
format_string("Value: %", {42});
format_string("health: 100%%");
```

If you need an argument adjacent to a percent sign (or next to another argument), use `%0` as an alias for `%`:

```go
hp := 67;
format_string("health: %0%%", {hp}); // "health: 67%"
```

For decimal rounding, use `format_float`:

```go
value := 3.14159;
format_string("pi: %", {format_float(value, decimals=2)}); // "pi: 3.14"
```

Common string helpers:

```go
clean := string_trim("  hello  ");
parts := string_split("a,b,c", ",");
name_lower := to_lower(player.get_username());
name_upper := to_upper("ready");
short := string_substring("abcdef", 1, 3); // "bcd"
```

## Logging

Logging follows the same formatting rules:

```go
log_info("Name: %, age: %", {player.get_username(), 12});
```

## Time

```go
current_time := get_time();       // float seconds since game start
frame := get_frame_number();      // current frame number
now_ns := get_nanoseconds_since_epoch();
utc := get_utc_datetime();
```

## SFX

```go
sound := get_asset(SFX_Asset, "sfx/click.wav");

desc := SFX.default_sfx_desc();
desc.entity_to_follow = entity.id;
desc.delay = 0.25;

sound_id := SFX.play(sound, desc);
SFX.stop(sound_id);
```

{% hint style="warning" %}
Play important long sounds server-only (`Game.is_server()`), otherwise misprediction can cause duplicated playback (music, death SFX, etc).
{% endhint %}

## Economy quick start (currencies)

Economy currencies are **per-player** and **automatically persisted**.

```go
coin_icon := get_asset(Texture_Asset, "ui/coin.png");
Economy.register_currency("Coins", coin_icon);

Economy.deposit_currency(player, "Coins", 10);
coins := Economy.get_balance(player, "Coins");

COST :: 50;
if Economy.can_withdraw_currency(player, "Coins", COST) {
    Economy.withdraw_currency(player, "Coins", COST);
}
```

See [Economy](/data-and-persistence/economy.md) for the full guide.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.allout.game/scripting/random-math-and-more.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
