Click here to Skip to main content
12,062,966 members (31,783 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Python
Hi, my issues is with trying to work with arrays that for each elements is a tuple of 2 values.

specifically the problem is to generate a random 2-dimensional walk of 200 (but for testing say 2) steps with a max distance, then 100 (try just 2) of those walks per stage, and commencing each stage at the largest distance from the origin of the previous stage.

I can successfully generate the array of random steps and get them to return the final position (x,y) value and as well calculate the distance of them from the origin of each walk:

That's defined in these functions:

#............................................................getPositionInteger
def getPosInt(description) :
    """Asks the user to enter a positive integer"""
    askAgain = False
    while askAgain == False:
        try:
            posInt = eval(raw_input("\n %s :  " %description))
            assert 0 < posInt , "Not a positive integer"
            assert type(posInt) == int , "Not a positive integer"
            askAgain = True
        except :
            print "Input failed, not a positive integer"
    return posInt
#...............................................................initialPosition
def initialPosition() :
    """Initial position of walker at the start of a random walk"""
    return (0.0, 0.0)
 
#......................................................................distance
def distance(posA, posB) :
    """Distance between two positions"""
    xi = posA[0] ; yi = posA[1]
    xf = posB[0] ; yf = posB[1]
    return np.sqrt((xf-xi)**2+(yf-yi)**2)
 
    #..................................................................getPositions
def getPositions(start, nSteps, maxStep):
    xArray = maxStep * np.random.random(nSteps+1)* np.cos(2.0 * np.pi * random.random())
    yArray = maxStep * np.random.random(nSteps+1)* np.sin(2.0 * np.pi * random.random())
    xArray[0] = start[0]
    yArray[0] = start[-1]
    xArray = np.cumsum(xArray)
    yArray = np.cumsum(yArray)
    return (xArray[-1], yArray[-1])
But I can't get the array of the final position of each walk per stage in (x,y) form per stage

Here's the main script and where I'm having trouble:
import numpy as np
import matplotlib.pylab as plt
import random
import time
 
MAX_STEP_SIZE = 0.90    # maximum size of a single step [m]
random.seed(12345)
 
#..........................................................................main
def main ():
    ''''''
    print "RANDOM WALK IN STAGES IN TWO DIMENSIONS"
    userdata = getDetails()
    print "\nPlease wait while random walks are generated and analyzed..."
    NUM_STAGES = userdata[0]
    NUM_WALKS = userdata[1]
    NUM_STEPS = userdata[2]
    stageStart = initialPosition()
    for stage in np.arange(NUM_STAGES):
        walks = np.zeros((NUM_WALKS, NUM_WALKS), dtype=np.ndarray)
        for walk in walks:
            walk = getPositions(stageStart, NUM_STEPS, MAX_STEP_SIZE)
            print walk
        print walks


You will see I'm having trouble making a an (x,y) style array, where the [0 0] should be [0.0 , 0.0] and its printed twice and additionally, its not changing to the final position.

I really appreciate and help, advice or references you can provide.
Thanks
-Sid
Posted 4-Apr-13 19:46pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160204.4 | Last Updated 5 Apr 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100