# Lua math.random problem

**URL:** <https://forum.defold.com/t/lua-math-random-problem/19372>\
**Category:** Questions\
**Created:** [April 27, 2018, 2:54pm UTC](https://forum.defold.com/t/lua-math-random-problem/19372 "2018-04-27T14:54:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![baloglub](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/baloglub/32/8495_2.png) [@baloglub](https://forum.defold.com/u/baloglub)\
**Post date:** [April 27, 2018, 2:54pm UTC](https://forum.defold.com/t/lua-math-random-problem/19372/1 "2018-04-27T14:54:51Z")

</div>

Hi guys, the game that I working on is creating blocks with position of x in some range. But I realized that on the start of the game it always pick the same value like every time it picks 320. What is the problem or how can I fix it?

---

<div class="post-metadata">

**Author:** ![AGulev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/agulev/32/4541_2.png) [@AGulev](https://forum.defold.com/u/AGulev)\
**Post date:** [April 27, 2018, 3:18pm UTC](https://forum.defold.com/t/lua-math-random-problem/19372/2 "2018-04-27T15:18:42Z")

</div>

You have to use math.randomseed before math.random  
More info [here](https://forum.defold.com/t/problem-with-random/1227/3)

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [April 27, 2018, 3:46pm UTC](https://forum.defold.com/t/lua-math-random-problem/19372/3 "2018-04-27T15:46:06Z")

</div>

Also should always discard first few random values as the first few will always be the same due to some C thing

math.random();math.random();math.random();math.random();

I prefer to use [defrng/defrng/mt.lua at master · subsoap/defrng · GitHub](https://github.com/subsoap/defrng/blob/master/defrng/mt.lua) for random numbers

---

<div class="post-metadata">

**Author:** ![Vincent](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/vincent/32/4357_2.png) [@Vincent](https://forum.defold.com/u/Vincent)\
**Post date:** [April 27, 2018, 6:28pm UTC](https://forum.defold.com/t/lua-math-random-problem/19372/4 "2018-04-27T18:28:18Z")

</div>

Is that a lua or a defold thing, because that is really annoying. 😕

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [April 27, 2018, 6:39pm UTC](https://forum.defold.com/t/lua-math-random-problem/19372/5 "2018-04-27T18:39:39Z")

</div>

If you’re talking about needing to clear the first few bad RNG it’s a Lua thing. Using MT avoids the issue.

---

<div class="post-metadata">

**Author:** ![88.josh](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/88.josh/32/20137_2.png) [@88.josh](https://forum.defold.com/u/88.josh)\
**Post date:** [April 27, 2018, 7:15pm UTC](https://forum.defold.com/t/lua-math-random-problem/19372/6 "2018-04-27T19:15:33Z")

</div>

It’s not really a defold or a lua thing, it is a computer thing. You should read more about it, because it’s really interesting\*

[https://www.howtogeek.com/183051/htg-explains-how-computers-generate-random-numbers/](https://www.howtogeek.com/183051/htg-explains-how-computers-generate-random-numbers/)

\*well i think it’s quite interesting

---

<div class="post-metadata">

**Author:** ![baloglub](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/baloglub/32/8495_2.png) [@baloglub](https://forum.defold.com/u/baloglub)\
**Post date:** [April 27, 2018, 9:27pm UTC](https://forum.defold.com/t/lua-math-random-problem/19372/7 "2018-04-27T21:27:34Z")

</div>

Thanks for reference, I am currently using it. However there is another problem

```lua
function init(self)
	self.last_pos = 100
	create(self, "#diamond-factory")
end

function create(self, fac)
	local pos = vmath.vector3()
	pos.y = 480
	local random_x = m.random(50, 590)
	while self.last_pos >= random_x - 100 or self.last_pos <= random_x + 100 do
		random_x = m.random(50, 590)
	end
	self.last_pos = random_x
	local pos_of_ball = go.get_position("/ball")
	pos.x = random_x
	factory.create(fac, pos)
end

```

I am using this script to pick values of long distance. If the position of the last one is between “prev\_x - 100 and prev\_x + 100” then it should pick new value until it is OK. But the game crashes because of infinite loop. What is the problem ? Or how can I fix it? Any idea?

---

<div class="post-metadata">

**Author:** ![dossy](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dossy/32/8490_2.png) [@dossy](https://forum.defold.com/u/dossy)\
**Post date:** [April 28, 2018, 6:55am UTC](https://forum.defold.com/t/lua-math-random-problem/19372/8 "2018-04-28T06:55:27Z")

</div>

> [@baloglub](#):
>
> ```lua
> local random_x = m.random(50, 590)
> while self.last_pos >= random_x - 100 or self.last_pos <= random_x + 100 do
> random_x = m.random(50, 590)
> end
> 
> ```

So, we start with `self.last_pos == 100`, so let’s figure out what values of `random_x` get us out of the loop.

- `self.last_pos >= random_x - 100` is `false` when `random_x >= 201`
- `self.last_pos <= random_x + 100` is `false` when `random_x < 0`

Since `random_x` can’t be less than `50`, it will never be less than `0`, so your `while` loop will never exit.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [April 28, 2018, 7:26am UTC](https://forum.defold.com/t/lua-math-random-problem/19372/9 "2018-04-28T07:26:39Z")

</div>

Why loop like that? Can’t you pass in the correct min and max values to math.random()?

---

<div class="post-metadata">

**Author:** ![baloglub](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/baloglub/32/8495_2.png) [@baloglub](https://forum.defold.com/u/baloglub)\
**Post date:** [April 28, 2018, 3:20pm UTC](https://forum.defold.com/t/lua-math-random-problem/19372/10 "2018-04-28T15:20:59Z")

</div>

There is a ball which is moving from left to right and right to left all the game time, so when the ball reaches a block, then the next block must be far away from ball’s position on the reach point. I don’t know if it is clear ☹

---

<div class="post-metadata">

**Author:** ![baloglub](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/baloglub/32/8495_2.png) [@baloglub](https://forum.defold.com/u/baloglub)\
**Post date:** [April 28, 2018, 3:21pm UTC](https://forum.defold.com/t/lua-math-random-problem/19372/11 "2018-04-28T15:21:41Z")

</div>

I gave different values to self.last\_pos but same result ☹

---

<div class="post-metadata">

**Author:** ![baloglub](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/baloglub/32/8495_2.png) [@baloglub](https://forum.defold.com/u/baloglub)\
**Post date:** [April 28, 2018, 3:25pm UTC](https://forum.defold.com/t/lua-math-random-problem/19372/12 "2018-04-28T15:25:56Z")

</div>

Sorry for flood but I solved the problem, It was my mistake…

```lua
	while random_x >= self.last_pos - 200 and random_x <= self.last_pos + 200 do
		random_x = m.random(50, 590)
	end

```

This worked fine while self.last\_pos is initially 320. Then the random\_x value should be between 120 and 520. So thanks for ideas 🙂

---

<div class="post-metadata">

**Author:** ![dossy](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dossy/32/8490_2.png) [@dossy](https://forum.defold.com/u/dossy)\
**Post date:** [April 28, 2018, 5:23pm UTC](https://forum.defold.com/t/lua-math-random-problem/19372/13 "2018-04-28T17:23:49Z")

</div>

Since you’re trying to ensure the delta (difference) between two objects is “large enough” you should rewrite the conditional to make that clear, like:

```lua
repeat
    random_x = math.random(50, 590)
until math.abs(self.last_pos - random_x) >= 200

```

That’s far easier to understand: keep generating a random number until the delta between the random and the `self.last_pos` is more than `200`.

---

<div class="post-metadata">

**Author:** ![88.josh](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/88.josh/32/20137_2.png) [@88.josh](https://forum.defold.com/u/88.josh)\
**Post date:** [April 30, 2018, 8:54am UTC](https://forum.defold.com/t/lua-math-random-problem/19372/14 "2018-04-30T08:54:35Z")

</div>

I am going to remember that bit of code. it is handy!

---

<div class="post-metadata">

**Author:** ![paxer](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/paxer/32/3529_2.png) [@paxer](https://forum.defold.com/u/paxer)\
**Post date:** [October 27, 2018, 2:49am UTC](https://forum.defold.com/t/lua-math-random-problem/19372/15 "2018-10-27T02:49:10Z")

</div>

@Pkeod thanks for the link! this module should really be included in Defold, the default math.random is very annoying due to this math.randomseed(os.clock()\*100000000000) and even with that from my test the module from your link works a lot better out of box!

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [October 27, 2018, 6:07am UTC](https://forum.defold.com/t/lua-math-random-problem/19372/16 "2018-10-27T06:07:57Z")

</div>

What are you doing to that poor seed?

Use math.randomseed(socket.gettime()) and optionally throw away the first random value.

---

<div class="post-metadata">

**Author:** ![paxer](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/paxer/32/3529_2.png) [@paxer](https://forum.defold.com/u/paxer)\
**Post date:** [October 27, 2018, 7:32am UTC](https://forum.defold.com/t/lua-math-random-problem/19372/17 "2018-10-27T07:32:43Z")

</div>

@britzl maybe “math.randomseed(socket.gettime())” works fine when I need to pick a number from the big range, but for my example, I just tested it, when I have 3 elements array (table) and I need to pick 1 random element from this array 3 times - one after another (my case is I just instantiate 3 objects but the object can have 3 different sprites and in init() of the object I assign one of the spites randomly) - “math.randomseed(socket.gettime())” works really bad unfortunately, basically almost always picking the same value for all 3 object (calls). When I use math.randomseed(os.clock()\*100000000000) - this works better but the linked library - works perfectly out of box without any additional lines…

I noticed that the default math.random works really bad when I need to pick a random element from some small array (small range of numbers math.random(1,3) for example) without this “os.clock()\*100000000000” madness , so basically analogue to [https://docs.yoyogames.com/source/dadiospice/002\_reference/maths/real%20valued%20functions/choose.html](https://docs.yoyogames.com/source/dadiospice/002_reference/maths/real%20valued%20functions/choose.html) which works excellent in GMS 2…

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [October 27, 2018, 8:32am UTC](https://forum.defold.com/t/lua-math-random-problem/19372/18 "2018-10-27T08:32:48Z")

</div>

Ah, I see. Well, hmm, if you throw away the first one or two random numbers you start to get the kind of variation that is to be expected:

```
-- os.time() seems to work equally well
math.randomseed(socket.gettime()) math.random() math.random()
print(math.random(1,3))

```

Not optimal. Perhaps we can deal with this better internally in the engine…

---

<div class="post-metadata">

**Author:** ![paxer](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/paxer/32/3529_2.png) [@paxer](https://forum.defold.com/u/paxer)\
**Post date:** [October 27, 2018, 9:07am UTC](https://forum.defold.com/t/lua-math-random-problem/19372/19 "2018-10-27T09:07:37Z")

</div>

exactly, it is a bit hacky, and given the number of questions on the forum about “Random does not work” I believe this is the problem which must be addressed given the fact how often random functionality needed in game development.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [October 27, 2018, 9:28am UTC](https://forum.defold.com/t/lua-math-random-problem/19372/20 "2018-10-27T09:28:05Z")

</div>

[![](https://img.youtube.com/vi/LDPMpc-ENqY/maxresdefault.jpg "rand() Considered Harmful") ](https://www.youtube.com/watch?v=LDPMpc-ENqY)

> **[PCG, A Family of Better Random Number Generators](https://www.pcg-random.org/)**
>
> PCG is a family of simple fast space-efficient statistically good algorithms for random number generation. Unlike many general-purpose RNGs, they are also hard to predict.

Here are some illustrations of “bad rng” drawing 4 colors randomly on a canvas I made a few years ago. I think the method was pick a random x,y and pick a random one of the 4 colors based on the RNG methods. So after a few seconds the patterns would emerge.

 ![2018-10-27%2002_20_38-exp](https://forum-defold-uploads.b-cdn.net/original/2X/f/f0a9f1ddf270695e0bd2ce1d639d1705a1be4472.png) ![2018-10-27%2002_19_50-exp](https://forum-defold-uploads.b-cdn.net/original/2X/5/5408a02417b7abb205c5aa4947dfbaaeb4298906.png)

And “better rng”

 ![2018-10-27%2002_21_45-exp](https://forum-defold-uploads.b-cdn.net/original/2X/2/29e757a91e6194f0777363d81a3ab9a97fcca0f8.png) ![2018-10-27%2002_21_32-exp](https://forum-defold-uploads.b-cdn.net/original/2X/3/354477354c5349a1682789ae014385f6bad1360d.png)
