> For the complete documentation index, see [llms.txt](https://read.bookofdefold.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://read.bookofdefold.com/defold-a-z/lua-tips/about-math.random.md).

# About math.random()

When you call math.random() without setting a seed you will get the same values each time every time you run your game. This is due to the way "random" values are generated. Generally you would set the seed to os.time() to make the "random" generation based on the current timestamp of the user, which functionally makes your random generation look to actually be random each time your game is ran, but would still generate the same random numbers each time if you used the same timestamp.

There is another issue however - the first few values generated by math.random() are usually the same no matter what. This is due to the way the builtin Lua RNG method generates numbers. To compensate this, you should always dispose of the initial values generated.

```lua
math.randomseed(os.time()) -- sets seed to the current time
math.random();math.random();math.random(); -- discards first few RNG numbers
print(math.random(), math.random(), math.random()) -- some random numbers between 0 and 1
print(math.random(0,1), math.random(0,1), math.random(0,1)) -- either 0 or 1 randomly
```

{% embed url="<https://repl.it/@pkeod/MathRandomExample>" %}


---

# 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:

```
GET https://read.bookofdefold.com/defold-a-z/lua-tips/about-math.random.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
