Ive written a program to do with drawing a line from a centre point and it is meant to snake randomly from the centre.

However, it seems to be incredibly biased as to where it decides to go.

the 4 directions are as simple as up down left and right, but it seems to only really head upwards. Yet i can see that some of the numbers do infact tell it to snake downwards, however it seems to make no real difference.

Any suggestions? (I do know there is some redundant code in there, but ill preen that later on.)

Running python 2.6

#
#
#
from graphics import *
def main():
    numWalks, numSteps, = getInputs()
    north,south,east,west = takeWalks(numWalks, numSteps)
   

def getInputs():
  
    numWalks = input("How many random walks to take? ")
    numSteps = input("How many steps for each walk? ")
    return numWalks, numSteps


def takeWalks(numWalks, numSteps):
    totalSteps = 0
    north=0
    south=0
    east=0
    west=0
    from math import sqrt
    from random import randint
    from graphics import *
    centre= Point(numWalks*numSteps,numWalks*numSteps)
    
    centrex,centrey=(numWalks*numSteps),(numWalks*numSteps)
    x,y=centrex,centrey
    win=GraphWin("",(numWalks*2)*numSteps,(numWalks*2)*numSteps)
    circle = Circle(centre, numWalks*numSteps)
    circle.setWidth(2)
    circle.draw(win)   
    

    for walk in range(numWalks):    
        
   
        direction = (randint(1,5)+randint(1,5)+randint(1,5)+randint(1,5))/4  
        for step in range(numSteps):
            
            if direction == 1:                
                
                startx,starty=x,y
                x,y+x,y+1
                line= Line(Point(startx,starty), Point(x,y))
                line.draw(win)
                
            elif direction == 2:
                
                startx,starty=x,y
                x,y=x,y-1
                line= Line(Point(startx,starty), Point(x,y))
                line.draw(win)
            elif direction == 3:
                
                startx,starty=x,y
                x,y=x+1,y
                line= Line(Point(startx,starty), Point(x,y))
                line.draw(win)
            else:
                
                startx,starty=x,y
                x,y=x-1,y
                line= Line(Point(startx,starty), Point(x,y))
                line.draw(win)
                
                
        print direction
        #y1=(north-south)
        #x1=(east - west)
        #distance = sqrt(x1**2 + y1**2)
            
    return north,south,east,west

def printExpectedDistance(distance):
    
    print "The expected number of steps away from the "
    print "start point is", int(distance)

main()

Can you elaborate on the need of
> direction = (randint(1,5)+randint(1,5)+randint(1,5)+randint(1,5))/4

Hint: this will give you neither better random, nor a uniform distribution.

Can you elaborate on the need of
> direction = (randint(1,5)+randint(1,5)+randint(1,5)+randint(1,5))/4

Hint: this will give you neither better random, nor a uniform distribution.

thats was one of the things i tried to do to give a better random based averages, however it also failed to work.

Im not too sure how the randint function works, so would all those numbers be the same, or different?

Member Avatar for masterofpuppets

hi,
why don' t you simplify this:

direction = (randint(1,5)+randint(1,5)+randint(1,5)+randint(1,5))/4

to this:

direction = randint( 1, 4 )
if direction == 1:
    ...
elif direction == 2:
    ...
# and so on. This should give you somewhat equal probability for all four numbers to appear.

The randint function returns a pseudo-random number in the range specified as argument, including both end points. That's why you should have randint( 1, 4 ) and not randint( 1, 5 ), since you only want movement in the four directions :)
Here's the documentation for the function:

>>> help ( randint )
Help on method randint in module random:

randint(self, a, b) method of random.Random instance
    Return random integer in range [a, b], including both end points.

>>>

hope this helps :)

So if im understanding this right, despite me having the randint function 4 times, its the same number for each one per loop? or will they be different?


I tried to do what the poster above suggested before resorting to what i have now. for diagnostic reasons i had the randint being output each loop so i could check that it was being displayed correctly. However, i found that no matter how many tests i did, if the limits were 1,4 it never put a single 4 out. with 1,5 4 does appear, but its still unable to snake downwards.

Muchly confused.

I ran this concept program a few times and it seems to behave perfectly random ...

# draw a series of lines in random directions
# using module graphics.py
# a Tkinter graphics wrapper from:
# http://mcsp.wartburg.edu/zelle/python/graphics.py

from graphics import *
from random import randint
from time import sleep

w = 500
h = 500
win = GraphWin("Random Lines", w, h)

# color list for color change option
# follows right, left, up, down directions
color_list = ['black', 'red', 'blue', 'green']

# number of lines
steps = 50
# length of line
length = 20
# start at center of window
x, y = w/2, h/2
for k in range(steps):
    direction = randint(1, 4)
    # go right
    if direction == 1:
        x2 = x + length
        y2 = y
    # go left
    elif direction == 2:
        x2 = x - length
        y2 = y    
    # go up
    elif direction == 3:
        x2 = x
        y2 = y - length
    # go down
    elif direction == 4:
        x2 = x
        y2 = y + length
    # now draw the line (optionally change color)
    line = Line(Point(x, y), Point(x2, y2))
    line.setFill(color_list[direction-1])
    line.draw(win)
    
    # set next starting point
    x, y = x2, y2
    # optional delay in seconds
    sleep(0.2)
    win.update()  # needed


# pause for click in window
win.getMouse()
win.close()

Right, so as I understand it

That concept program you put up would fit where i currently have my "takeWalks" function? From what i can see, it looks like you did pretty much what i did. Could if be the lack of a delay that causes the issues? or perhaps the fact that i dont technically reset the values?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.