# How to set random colour once ?

Hi there,

I have been playing with the particle example to try and learn a bit more.

more.

``````    p={}
ps=500

-- Use this function to perform your initial setup
function setup()
print("Hello World!")
for i=0,ps do

p[i]= {x=math.random(WIDTH*10)/10,
y=math.random(HEIGHT*10)/10, ox=0.0, oy=0.0, vx=math.random(20)-10,
vy=math.random(20)-10}

end
end

-- This function gets called once every frame
function draw()
noSmooth()

background(10,10,20)
fill(255,0,0)

for i=0,ps do

stroke(math.random(0,255), math.random(0,255), math.random(0,255), 255)
strokeWidth(3)

p[i].ox= p[i].x
p[i].oy= p[i].y
p[i].x = p[i].x + p[i].vx
p[i].y = p[i].y + p[i].vy

if p[i].x<0 then
p[i].x=0
p[i].vx= -p[i].vx
end

if p[i].y<0 then
p[i].y=0
p[i].vy= -p[i].vy
end

if p[i].x>WIDTH then
p[i].x=WIDTH
p[i].vx= -p[i].vx
end

if p[i].y>HEIGHT then
p[i].y=HEIGHT
p[i].vy= -p[i].vy
end

p[i].vx = p[i].vx*0.98
p[i].vy = p[i].vy*0.98

line(p[i].ox, p[i].oy, p[i].x, p[i].y)
end
end

function touched(t)
a=10
for i=0,ps do
d= (p[i].x-t.x)*(p[i].x-t.x) + (p[i].y-t.y)*(p[i].y-t.y)
d= math.sqrt(d)
p[i].vx = p[i].vx - a/d*(p[i].x-t.x)
p[i].vy = p[i].vy - a/d*(p[i].y-t.y)

end
end
``````

The problem is that it changes the colour of each dot each time instead of setting the colour randomly once (if that makes sense)

How would I go about just settings a random colour for each dot just once so it doesn’t change ?

Thanks
Lee

Can you put 3 tildas ~ like this: — at the beginning and end of your code?

Thank you, I was trying to work that one out lol

Do that: (nice effects, btw): i transfered the color creation in the setup, in a table, like you ‘p’

``````
--# Main
p={}
ps=500

-- Use this function to perform your initial setup
function setup()
print("Hello World!")
c={}
for i=0,ps do

p[i]= {x=math.random(WIDTH*10)/10,
y=math.random(HEIGHT*10)/10, ox=0.0, oy=0.0, vx=math.random(20)-10,
vy=math.random(20)-10}
c[i]=color(math.random(0,255), math.random(0,255), math.random(0,255), 255)
end
end

-- This function gets called once every frame
function draw()
noSmooth()

background(10,10,20)
fill(255,0,0)

for i=0,ps do

stroke(c[i])
strokeWidth(3)

p[i].ox= p[i].x
p[i].oy= p[i].y
p[i].x = p[i].x + p[i].vx
p[i].y = p[i].y + p[i].vy

if p[i].x<0 then
p[i].x=0
p[i].vx= -p[i].vx
end

if p[i].y<0 then
p[i].y=0
p[i].vy= -p[i].vy
end

if p[i].x>WIDTH then
p[i].x=WIDTH
p[i].vx= -p[i].vx
end

if p[i].y>HEIGHT then
p[i].y=HEIGHT
p[i].vy= -p[i].vy
end

p[i].vx = p[i].vx*0.98
p[i].vy = p[i].vy*0.98

line(p[i].ox, p[i].oy, p[i].x, p[i].y)
end
end

function touched(t)
a=10
for i=0,ps do
d= (p[i].x-t.x)*(p[i].x-t.x) + (p[i].y-t.y)*(p[i].y-t.y)
d= math.sqrt(d)
p[i].vx = p[i].vx - a/d*(p[i].x-t.x)
p[i].vy = p[i].vy - a/d*(p[i].y-t.y)

end
end
``````

If you in the beginning of the draw function set a random seed value, then I think it will work for you.