Create accumulated random movement of a point

Hi,
I’m trying to create a random moving point using for loop but the script outputs always all the points in the same location although the print command says otherwise. If someone could explain why this is happening I’ll be thankful.

NB: in the attached gh file I created two examples in another way that makes the script work fine but my question here is why I have those messages in the panel that doesn’t match with the outputs?


import rhinoscriptsyntax as rs
import random as r

#initial point 
pt = rs.AddPoint(0,0,0)
#point list creation
listPt = []

#if condition for button as seed
if toggle == False:
    #initializing for loop to move the initial point in random directions
    for i in range (0,iteration):
        pt1= rs.coerce3dpoint(pt)
        v = rs.CreateVector(r.randint(-20,20), r.randint(-20,20), r.randint(-20,20))
        print ("intial Point location for loop number " + str(i+1) + " is " + str(pt1))
        print ("random vector is " + str(v))
        movedPt = rs.MoveObject(pt, v)
        listPt.append(movedPt)
        pt = movedPt
    #out putting the list
    OutPts = listPt


accumulated random movement of a point.gh (18.3 KB)

Hi @Faried,

Simple typo! make this change:

Regards
Jeremy

1 Like

Thanks @jeremy5 for replying.
Could you please explain to me why pt only doesn’t work ? because the statement in line 17 seems logic to me because pt is always updating at the end of each loop and it stores the value of the new moved point.
Sorry if the question seems a bit trivial but it’s my first steps in learning python gh and I don’t quiet get the reason why this is happening.

pt is a point object whereas pt1 is a RhinoCommon point3D object. As I understand it, that distinction matters when a RhinoScriptSyntax command invokes the underlying RhinoCommon routines which, unlike Python, are strongly typed.

I think the distinction and where to use each object type is hard to get one’s head around: the best thing is to experiment. Perhaps one of the forum’s heavyweight coders will explain it better than I can.

Here’s a version of your gh file with some additional diagnostic statements to illustrate the difference:
accumulated random movement of a point with diagnostics.gh (31.7 KB)

HTH
Jeremy

1 Like

Thanks a bunch Jeremy, that was very helpful :pray:

1 Like

Here’s a basic random walk using RhinoCommon, that might help:

import Rhino as rc
import random as rd

# Make empty polyline and add start point
pl = rc.Geometry.Polyline()
pl.Add(0,0,0)

# Set random seed
rd.seed(Seed)

# Perform random walk
for i in range(Steps):
    
    # Make random vector
    x = rd.uniform(-1,1)
    y = rd.uniform(-1,1)
    z = rd.uniform(-1,1)
    rdVec = rc.Geometry.Vector3d(x,y,z)
    
    # Get/move/add polyline point by random vector
    newPt = pl.Last + rdVec
    pl.Add(newPt)
    
# Cast and output polyline to Grasshopper
Walk = pl.ToNurbsCurve()

And the same logic implemented as a dynamic model (was a little exercise in a course I taught, demonstrating a bunch of more advanced topics):


220905_DynamicWalk_00.gh (8.6 KB)

3 Likes

In case you haven’t seen them, here are a couple of useful pages from the Rhino Developer documentation:
Rhino - Rhino objects in Python (rhino3d.com)
Rhino - Points in Python (rhino3d.com)

1 Like

Thanks @AndersDeleuran, this is much more simple than mine and very informative I really appreciate it :pray:

1 Like