> For the complete documentation index, see [llms.txt](https://fehdead.gitbook.io/npc-kit/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://fehdead.gitbook.io/npc-kit/vm-reference/calls/rand_29.md).

# npc.rand\_29

```lua
npc.rand_29(max)
```

|                         |                                                                        |
| ----------------------- | ---------------------------------------------------------------------- |
| id                      | `0x0029`                                                               |
| arguments               | 1: Int                                                                 |
| result                  | Int: goes to the very next call, or to `npc.lua(npc.rand_29(...), fn)` |
| in NPC Kit              | yes                                                                    |
| measured from a kit NPC | returns                                                                |
| vanilla scripts         | 6729 calls in 355 of 1114 scripts                                      |

## What it does

Gives a random number from `0` to `max - 1`. A result.

## Arguments

### `max` (Int)

How many values. Vanilla: `5`, `100`, `15`, `3`, `20`, `30` most often.

## Result

`0` to `max - 1`, from the game's own generator.

## Timing

Returns at once.

## In NPC Kit

The result goes to the very next call as its first argument, so a random wait works in a recorded script:

```lua
main = function()
    npc.wait(npc.rand_29(120));   -- 0 to 119 frames
    npc.stdmotionplay_2c2(0x01000013, 20);
    npc.wait(60);
    npc.stdmotionplay_2c2(0x01000000, 20);
end,
```

For anything else (a random line, a random choice), use Lua's `math.random` in `npc.lua`.

## Used with

* [wait](https://fehdead.gitbook.io/npc-kit/vm-reference/calls/wait.md): a random pause in a routine.

## Example

A villager who laughs at random moments: the wait before each laugh is random, the result fed straight into `wait`.

```lua
main = function()
    npc.wait(npc.rand_29(180));              -- 0 to 179 frames
    npc.stdmotionplay_2c2(0x01000013, 20);   -- laugh
    npc.wait(60);
    npc.stdmotionplay_2c2(0x01000000, 20);
    npc.wait(60);
end,
```

## Pitfalls

* A `wait(0)` is possible: keep the fixed `wait(60)` in `main`, so it always gives frames back.
