[solved] math.random problem
Posted: Tue Sep 06, 2011 3:45 am
Hey, I'm very new to Lua and LOVE and this forum ... so I've got this piece of code:
(I use that code inside some more code in a LOVE project but I put it alone to run it faster in Lua)
It's supposed to make three random 'enemies' with some properties (x, y, radius, ...) but it keep giving me three groups of the exact same numbers. If you run that on Lua you get something like this:
I need the three groups to be different. I hope you can help me.
Code: Select all
function new_enemies()
local enemy = {}
for i = 1,nof_enemies do
enemy.radius = math.random(10, 35)
enemy.x = math.random(0 + enemy.radius, 800 - enemy.radius)
enemy.y = math.random(0 + enemy.radius, 200)
enemy.segments = math.random(4, 15)
table.insert(enemies, enemy)
end
end
math.randomseed(os.time())
enemies = {}
nof_enemies = 3
new_enemies()
print("i, e.radius, e.x, e.y, e.segments")
for i,e in ipairs(enemies) do
print(i, e.radius, e.x, e.y, e.segments)
end
(I use that code inside some more code in a LOVE project but I put it alone to run it faster in Lua)
It's supposed to make three random 'enemies' with some properties (x, y, radius, ...) but it keep giving me three groups of the exact same numbers. If you run that on Lua you get something like this:
Code: Select all
C:\Users\Raul\p\lua>rnd.lua
i, e.radius, e.x, e.y, e.segments
1 27 216 43 8
2 27 216 43 8
3 27 216 43 8
C:\Users\Raul\p\lua>rnd.lua
i, e.radius, e.x, e.y, e.segments
1 15 643 22 8
2 15 643 22 8
3 15 643 22 8
I need the three groups to be different. I hope you can help me.