Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python code help! roll a 6 sided die to choose a random edgepoint to start (alre

ID: 3694331 • Letter: P

Question

Python code help! roll a 6 sided die to choose a random edgepoint to start (already in code) if the event is 1 or 2 choose edge point A, 3 or 4 choose edgepoint B, 5 or 6 choose edgepoint C. move from initial position to mid point between edgepoint selected by random die roll and inital position. repeat 30 times with use of while loop......i'm stuck on the while loop, i've made 3 dots, but thats all. please help

import numpy as np
import pylab as plt

##############################################################################
# some helpful functions
##############################################################################
def plot_point(p,ic=0):
colorlist=['red','green','blue']
plt.plot(p[0],p[1],'.',color=colorlist[ic],markersize=4)
return
  
def distance(p1,p2):
"""distance between two points p1 p2. p1 and p2 are numpy arrays of size 2"""
d=np.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)
return d

def step(p1,p2):
"""go from p1 to p2 a step of some fraction of the distance towards p2, and returns the new point"""
#d=distance(p1,p2)
# remember we go half distance from p1 to p2: this is a line defined by
# the point p1 and the slope
dx=p2[0]-p1[0]
dy=p2[1]-p1[1]
frac=1.0/9.0
return np.array([p1[0]+dx*frac,p1[1]+dy*frac])


#############################################################################
# main part of the program
#############################################################################
# define an equilateral triangle
# see wikipedia (https://en.wikipedia.org/wiki/Equilateral_triangle)
# side length is a
# => h height of the triangle
# a^2=h^2+ 1/4*a^2
# => h^2=3/4*a^2
# => h=sqrt(3)/2a
#
a=1.0
h=np.sqrt(3)/2.0*a
edgepoint=np.empty([3,2])
edgepoint[0,]=[-1/2.*a, 0]
edgepoint[1,]=[1/2.*a, 0]
edgepoint[2,]=[0, h]
x=edgepoint[[0,1,2,0],0]
y=edgepoint[[0,1,2,0],1]
plt.plot(x,y)

# seed with a first random point, first the x-axis
x0=np.random.uniform(low=-1/2.0*a,high=1/2.0*a,size=1)
# now the y-coordinate, for this we need to define an upper
# limit for the random number
# per default np.random.uniform() returns numbers between 0 and 1
if (x0>=0):
ymax=h-x0*2*h/a
else:
ymax=(a/2+x0)*2*h/a
  
y0=np.random.uniform(low=0.,high=ymax,size=1)
p0=np.array([x0,y0])
plot_point(p0)

test=np.random.random_integers(0,2,1)
newx=x[test]
newy=y[test]
p2=np.array([newx,newy])


p0=step(p0,p2)
plot_point(p0,ic=1)


p0=step(p0,p2)
plot_point(p0,ic=2)

Explanation / Answer

import numpy as np

import pylab as plt

##############################################################################

# some helpful functions

##############################################################################

def plot_point(p,ic=0):

    colorlist=['red','green','blue']

    plt.plot(p[0],p[1],'.',color=colorlist[ic],markersize=4)

    return

def distance(p1,p2):

    """distance between two points p1 p2. p1 and p2 are numpy arrays of size 2"""

    d=np.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)

    return d

def step(p1,p2):

    """go from p1 to p2 a step of some fraction of the distance towards p2, and returns the new point"""

    #d=distance(p1,p2)

    # remember we go half distance from p1 to p2: this is a line defined by

    # the point p1 and the slope

    dx=p2[0]-p1[0]

    dy=p2[1]-p1[1]

    frac=1.0/9.0

    return np.array([p1[0]+dx*frac,p1[1]+dy*frac])

#############################################################################

# main part of the program

#############################################################################

# define an equilateral triangle

# see wikipedia (https://en.wikipedia.org/wiki/Equilateral_triangle)

# side length is a

# => h height of the triangle

# a^2=h^2+ 1/4*a^2

# => h^2=3/4*a^2

# => h=sqrt(3)/2a

#

a=1.0

h=np.sqrt(3)/2.0*a

edgepoint=np.empty([3,2])

edgepoint[0,]=[-1/2.*a, 0]

edgepoint[1,]=[1/2.*a, 0]

edgepoint[2,]=[0, h]

x=edgepoint[[0,1,2,0],0]

y=edgepoint[[0,1,2,0],1]

plt.plot(x,y)

# seed with a first random point, first the x-axis

n=0

while n<30:

x0=np.random.uniform(low=-1/2.0*a,high=1/2.0*a,size=1)

# now the y-coordinate, for this we need to define an upper

# limit for the random number

# per default np.random.uniform() returns numbers between 0 and 1

if (x0>=0):

    ymax=h-x0*2*h/a

else:

    ymax=(a/2+x0)*2*h/a

y0=np.random.uniform(low=0.,high=ymax,size=1)

p0=np.array([x0,y0])

plot_point(p0)

test=np.random.random_integers(0,2,1)

newx=x[test]

newy=y[test]

p2=np.array([newx,newy])

p0=step(p0,p2)

plot_point(p0,ic=1)

p0=step(p0,p2)

plot_point(p0,ic=2)

n+=1