Jump to content

Generate random number in Python Scripting


Recommended Posts

Hello,

It's my first post here, so please be gentle 🙂

I wanted to create my first script in Vectorworks using Python, simple Random Walker like this:
 

import random

size = 50
posX = 0
posY = 0
steps = 100

for i in range(steps):
    vs.Rect(posX, posY, posX + size, posY + size)
    r = random(4)
    if r == 0:
        posX += size
        posY += size
    if r == 1:
        posX -= size
        posY += size
    if r == 2:
        posX -= size
        posY -= size
    if r == 3:
        posX += size
        posY -= size

How can I create random int number in range 0 - 3 (0, 1, 2, 3).  Same code runs in processing.

There is this method in python, but it does not work in Vectorworks.

 randrange ([start,] stop [,step])

Thanks for any help!

Link to comment
  • Marionette Maven

Maybe this will help - you need to use "random.randrange(value)"

 

import random

size = 50
posX = 0
posY = 0
steps = 100

for i in range(steps):
    vs.Rect(posX, posY, posX + size, posY + size)
    r = random.randrange(4) #corrected to include namespace
    if r == 0:
        posX += size
        posY += size
    if r == 1:
        posX -= size
        posY += size
    if r == 2:
        posX -= size
        posY -= size
    if r == 3:
        posX += size
        posY -= size
  • Like 1
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...