# Basic stochastic model

Hello all. I am trying to simulate stochasitc motions for some particles in a specific domain. It is fine for just 2 particles. But if I would like to simulate more than 2 particles, my code becomes too slow.(I only know the method to keep adding xxx,newxxx, and new while to create new particle motion) Is there any suggestion to help to get better performance? Here is my code:

``````using Luxor
using Plots

const x0=-30
const x1=-15
const x2=-5
const x3=15
const x6=30
const y0=-30
const y1=-25
const y2=-20
const y3=0
const y6=30

const domain = [Point(x0, y0), Point(x1, y0), Point(x1, y3), Point(x3, y3), Point(x3, y1), Point(x2, y1), Point(x2, y0),
Point(x6, y0), Point(x6, y6), Point(x0, y6)]

const cave = [Point(x2,y0),Point(x2,y1),Point(0,y0),Point(0,y1)]

function plotpolygon(polygon)
plot(Tuple.([polygon; polygon[1]]), legend=false)
# points=getlines(poly)
# allpoints=[points; points[1]]

# plot(show=false, legend=false)
# for l in getlines(poly)
#     a=geta(l)
#     b=getb(l)
#     plot!([getx(a), getx(b)], [gety(a), gety(b)], color=:blue)
# end
# plot!(show=true)
end

isoutside(x,y)=~isinside(Point(x,y), domain;allowonedge=true)

#plot(Tuple.(polygon), legend=false)
#scatter!(Tuple.(points), marker_z=inside)

function main()
x=10
y=10
xx=11
yy=11
for i=1:1000
newx=newy=-1000
newxx=newyy=-1000
while isoutside(newx, newy)
newx= rand()<0.5 ? x+1 : x-1
newy= rand()<0.5 ? y+1 : y-1
end
while isoutside(newxx, newyy)
newxx= rand()<0.5 ? xx+1 : xx-1
newyy= rand()<0.5 ? yy+1 : yy-1
end
x=newx
y=newy
xx=newxx
yy=newyy
plotpolygon(domain)
scatter!([x,xx],[y,yy],markercolor =[:blue,:red], show=true, leg=false)
sleep(0.03)
end
end

main()
``````

Many thanks!