# My random direction is not so random

Hello guys, i have some problems with random floating objects around screen:
I have table with 20 elements and for each element of table chooses random spawn point and random speed on x and y axis based on coordinated of spawn point. Heres my class code

``````Asteroids = class()

function Asteroids:init(x)
-- you can accept and set parameters here

--my main table

self.asteroids = {}
--spawn points table
self.points = {vec2(0,0),vec2(0,HEIGHT/2),vec2(0,HEIGHT),vec2(WIDTH/2,0),vec2(WIDTH/2,HEIGHT),vec2(WIDTH,0),vec2(WIDTH,HEIGHT/2),vec2(WIDTH,HEIGHT)}
--  print(#self.points)
self:insert()
end

function Asteroids:draw()
-- Codea does not automatically call this method
--just a timer
local time = math.floor(zero*10)

--iterate through table
for k,v in pairs(self.asteroids) do

--make my asteroids float
v["start"].x = v["start"].x + v["speed"].x
v["start"].y = v["start"].y + v["speed"].y

--just chooses one of two random pictures of asteroids
if v["size"] == 1 then
sprite("Space Art:Asteroid Small",v["start"].x,v["start"].y)
else
sprite("Space Art:Asteroid Large",v["start"].x,v["start"].y,140/2,116/2)
end

-- oob = Out Of Bounds
if v["start"].x > WIDTH+40 or v["start"].x < -40 or v["start"].y > HEIGHT+40 or v["start"].y < -40 then
v["oob"] = true
end

if v["oob"] == true then

end
end
--  print(#self.asteroids)
end

--now is the funny part
--heres code , which insert table with values of asteroid (coordinates, speed and etc)
--dont be scary about self.asteroids[i]["start"] i just use named values on table so this is how i call it in loop
--in this loop i fill my self.asteroids with values i described before and check where is spawn point and calculate random speed from this point
--but on screenshots below you can see what one sprite cover another
--and i want to avoid this overlaying
--i found, what numbers with float dot is very near to what i want, but first problem of this method is what sprite will go very slow and second is what it still covers another asteroid

function Asteroids:insert()
for i = 1,20 do
table.insert(self.asteroids,{start = self.points[math.random(#self.points)],oob = false,speed = vec2(0,0),size = 1})
if self.asteroids[i]["start"] == vec2(0,0) or self.asteroids[i]["start"] == vec2(0,HEIGHT/2) or self.asteroids[i]["start"] == vec2(WIDTH/2,0) then
self.asteroids[i]["speed"].x = math.random(1,3)
self.asteroids[i]["speed"].y = math.random(1,3)
elseif self.asteroids[i]["start"] == vec2(0,HEIGHT) or self.asteroids[i]["start"] == vec2(WIDTH/2,HEIGHT) then
self.asteroids[i]["speed"].x = math.random(1,3)
self.asteroids[i]["speed"].y = math.random(-3,-1)
elseif self.asteroids[i]["start"] == vec2(WIDTH,0) or self.asteroids[i]["start"] == vec2(WIDTH/2,0) or self.asteroids[i]["start"] == vec2(WIDTH,HEIGHT/2) then
self.asteroids[i]["speed"].x = math.random(-3,-1)
self.asteroids[i]["speed"].y = math.random(1,3)
elseif self.asteroids[i]["start"] == vec2(WIDTH/2,HEIGHT) or self.asteroids[i]["start"] == vec2(WIDTH,HEIGHT/2) or self.asteroids[i]["start"] == vec2(WIDTH,HEIGHT) then
self.asteroids[i]["speed"].x = math.random(-3,-1)
self.asteroids[i]["speed"].y = math.random(-3,-1)
end
if math.random(0,1) == 1 then
self.asteroids[i]["size"] = 1
else
self.asteroids[i]["size"] = 0
end
--    print(self.asteroids[i]["speed"])
end
end

function Asteroids:touched(touch)
-- Codea does not automatically call this method
end

``````

@lupino8211 All of your random numbers are 1 to 3, -3 to -1, or 0 to 1. Thatâ€™s not giving you much of a random number. You need to modify your code to use a larger range of random numbers.

@dave1707 oh yes, i didnt noticed this, thank you

@lupino8211 Are you after something like this.

``````displayMode(FULLSCREEN)

function setup()
astTab={}
for z=1,20 do
if math.random(100)<50 then
ty="Space Art:Asteroid Large"
else
ty="Space Art:Asteroid Small"
end
table.insert(astTab,{pos=vec2(math.random(WIDTH),math.random(HEIGHT)),
dir=vec2(math.random(-100,100),math.random(-100,100)),type=ty,
speed=math.random(5)})
end
end

function draw()
background(0)
for a,b in pairs(astTab) do
sprite(b.type,b.pos.x,b.pos.y)
v1=b.dir
v1=v1:normalize()
b.pos=b.pos+v1*b.speed
if b.pos.x<-40 then b.pos.x=WIDTH+40 end
if b.pos.x>WIDTH+40 then b.pos.x=-40 end
if b.pos.y<-40 then b.pos.y=HEIGHT+40 end
if b.pos.y>HEIGHT+40 then b.pos.y=-40 end
end
end
``````

@dave1707 thank you, that is exactly what i want