# Math.random reusing same values?

**URL:** <https://forum.defold.com/t/math-random-reusing-same-values/76642>\
**Category:** Questions\
**Created:** [April 17, 2024, 8:03pm UTC](https://forum.defold.com/t/math-random-reusing-same-values/76642 "2024-04-17T20:03:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hauger987](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/hauger987/32/35584_2.png) [@Hauger987](https://forum.defold.com/u/Hauger987)\
**Post date:** [April 17, 2024, 8:03pm UTC](https://forum.defold.com/t/math-random-reusing-same-values/76642/1 "2024-04-17T20:03:51Z")

</div>

Hi all,

I’m currently trying to create a factory that will randomly set the locations of the created objects. I’ve tried two methods to create these random values. One of them makes each value different, and the other results in all three values being the same random number.

```lua
math.randomseed(os.time())
factory.create("/worm_spawner#worm_factory", vmath.vector3(250,250,1), nil, { local_pos = math.random(-75, 75), bool = math.random(0, 1)}, .25)
factory.create("/worm_spawner#worm_factory", vmath.vector3(200,200,1), nil, { local_pos = math.random(-75, 75), bool = math.random(0, 1)}, .25)
factory.create("/worm_spawner#worm_factory", vmath.vector3(550,400,1), nil, { local_pos = math.random(-75, 75), bool = math.random(0, 1)}, .25)

```

V1: all three local\_pos values are the same

```lua
math.randomseed(os.time())
x1 = math.random(-75, 75)
x2 = math.random(-75, 75)
x3 = math.random(-75, 75)
factory.create("/worm_spawner#worm_factory", vmath.vector3(250,250,1), nil, { local_pos = x1, bool = math.random(0, 1)}, .25)
factory.create("/worm_spawner#worm_factory", vmath.vector3(200,200,1), nil, { local_pos = x2, bool = math.random(0, 1)}, .25)
factory.create("/worm_spawner#worm_factory", vmath.vector3(550,400,1), nil, { local_pos = x3, bool = math.random(0, 1)}, .25)

```

V2: all three local\_pos values are different

My best guess is that the program is calculating one random value with the given parameters and using it for all the instances where it’s called. Is there any way to make the first version work? (I’d like to use loops in the final version of this). Thanks!

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [April 17, 2024, 8:40pm UTC](https://forum.defold.com/t/math-random-reusing-same-values/76642/2 "2024-04-17T20:40:27Z")

</div>

You have to throw away the first one or two random numbers.

> [@Lua math.random problem](https://forum.defold.com/t/lua-math-random-problem/19372/18):
>
> 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…

```lua
math.randomseed(socket.gettime()) 
math.random() 
math.random()
math.random()

```

---

<div class="post-metadata">

**Author:** ![Hauger987](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/hauger987/32/35584_2.png) [@Hauger987](https://forum.defold.com/u/Hauger987)\
**Post date:** [April 17, 2024, 8:47pm UTC](https://forum.defold.com/t/math-random-reusing-same-values/76642/3 "2024-04-17T20:47:39Z")

</div>

I don’t understand how that relates to this. In both of these cases, I’m generating the same amount of random numbers, I’m just changing when I do it and how I use them, and getting different results.

---

<div class="post-metadata">

**Author:** ![iotaHum](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/i/977dab/32.png) [@iotaHum](https://forum.defold.com/u/iotaHum)\
**Post date:** [April 17, 2024, 10:26pm UTC](https://forum.defold.com/t/math-random-reusing-same-values/76642/4 "2024-04-17T22:26:44Z")

</div>

I think maybe in the first case math.random() is being passed inside the table to the factory function; when the table element is accessed it is run, in this name-space math.random( os.time()) has not been run, resulting in all the values being the same.

---

<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 18, 2024, 5:25am UTC](https://forum.defold.com/t/math-random-reusing-same-values/76642/5 "2024-04-18T05:25:57Z")

</div>

> [@selimanac](#):
>
> You have to throw away the first one or two random numbers.

This should no longer be needed. We fixed that quite some time ago

---

<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 18, 2024, 5:29am UTC](https://forum.defold.com/t/math-random-reusing-same-values/76642/6 "2024-04-18T05:29:00Z")

</div>

> [@Hauger987](#):
>
> `math.randomseed(os.time())`

You should only call this function once when your program starts. Are you calling it multiple times?
