G1R:MP Forum Alpha
Log in Join
Gothic 1 Remake Multiplayer

The Colony Forum

News, support, development discussions, server communities, and stories from beyond the Barrier.

Colony discussion

Random Number

Started by Xardas0327 in Suggestions

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
09-13-2026, 05:04 PM
#1
It would be good if we have random number generator.

For example:
Quote:getRandomInt(includeMinInt, excludeMaxInt) -- Maybe it also should be fine if the top number is include too.

and
Quote:getRandomFloat (includeMinFloat, includeMaxFloat ) -- The top number should be included here.

Note:
I tried to use this code. But if I use this code, the server drop my character and the server reconnected it. (even on 0.1.3)
Moreover, this is not true random. I should use something similar like this math.randomseed(os.time()), but the os is not available from 0.1.3.
Quote:local startPositions = {
    { x = 1116.75, y = -1007.51, z = -36.66 },
    { x = 1106.01, y = -1049.35, z = -29.86 },
    { x = 1100.28, y = -1050.55, z = -35.44 },
    { x = 1115.40, y = -1049.39, z = -37.10 },
    { x = 1161.29, y = -1050.89, z = -42.04 },
    { x = 1154.55, y = -1011.29, z = -42.16 },
}

addCommandHandler("castle", function(playerId, commandName)
    local randomIndex = math.random(1, #startPositions)
    setPlayerPosition(
        playerId,
        startPositions[randomIndex].x,
        startPositions[randomIndex].y,
        startPositions[randomIndex].z
    )
end)
09-13-2026, 05:45 PM
#2
Lua 5.1 already provides a built-in pseudo-random number generator through the `math` library.

Available functions:

```lua
math.random()
```

Returns a pseudo-random floating-point number between `0` and `1`.

```lua
math.random(max)
```

Returns a pseudo-random integer between `1` and `max`.

Example:

```lua
local number = math.random(100)
print(number) -- 1-100
```

You can also specify both the minimum and maximum value:

```lua
math.random(min, max)
```

Example:

```lua
local number = math.random(10, 50)
print(number) -- 10-50
```

It's also recommended to initialize the random seed once when the script starts:

```lua
math.randomseed(os.time())
```

Full example:

```lua
math.randomseed(os.time())

local randomNumber = math.random(1, 100)

print("Random number: " .. randomNumber)
```

So unless there is a specific reason to implement a custom random number generator, Lua 5.1's built-in `math.random()` should be enough for most gameplay/script purposes.
09-13-2026, 05:45 PM
#3
Lua 5.1 already provides a built-in pseudo-random number generator through the `math` library.

Available functions:

```lua
math.random()
```

Returns a pseudo-random floating-point number between `0` and `1`.

```lua
math.random(max)
```

Returns a pseudo-random integer between `1` and `max`.

Example:

```lua
local number = math.random(100)
print(number) -- 1-100
```

You can also specify both the minimum and maximum value:

```lua
math.random(min, max)
```

Example:

```lua
local number = math.random(10, 50)
print(number) -- 10-50
```

It's also recommended to initialize the random seed once when the script starts:

```lua
math.randomseed(os.time())
```

Full example:

```lua
math.randomseed(os.time())

local randomNumber = math.random(1, 100)

print("Random number: " .. randomNumber)
```

So unless there is a specific reason to implement a custom random number generator, Lua 5.1's built-in `math.random()` should be enough for most gameplay/script purposes.
09-13-2026, 08:08 PM (This post was last modified: 09-13-2026, 08:09 PM by Xardas0327.)
#4
I know this.
It would be good, if this code could work.

But this code doesn't work:
Quote:math.randomseed(os.time())

I get this error:
Quote:attempt to index global 'os' (a nil value)

And you can read it on wiki:
The os is unavailable. (and many other packages too)
You can see it under the Lua VM and execution.
https://g1r-mp.com/wiki/index.php?title=...ing_limits

But if the os is active from the next version (0.1.4 or similar), it is fine for me.
Forum Jump: