lundi 28 mars 2016

(Python) Creating 'X' points of form p = [a,b,c] using a for loop and then randomizing a,b,c within p = [a,b,c] 'Y' times using a second for loop?

I think that this question could be answered by all, but to give more background I am using python to code for rhino. My goal is to create a number of curves made out of X points. The points also have to be randomly located within a 10 by 80 by 20 unit rectangular prism. I started with this:

import rhinoscriptsyntax as rs
import random

crvNumber = 300
numRandPts = 5
xLength = 10
yLength = 80
zLength = 20
crvArray = []
crvPtArray = []

for j in range(0,numRandPts):

    ptj = [xLength*random.random(),yLength*random.random(),zLength*random.random()]
    crvPtArray.append(ptj)

for i in range(0,crvNumber):

    curves = rs.AddCurve (crvPtArray, degree = numRandPts)
    crvArray.append(curves)

I'm sure you can spot immediately that this won't work because the generation of the random points occurs within the first for loop, and as a result, the second for loop simply outputs the same random curve 300 times. Does anyone have any idea how I could generate a specific number of points with the first loop and then actually assign their values in the second for loop such that I end up with 300 curves with j random points? I've been researching this for the past 2 hours to no avail. My first workaround idea was that maybe it would be simpler to just generate a set of say 1000 random points and then choose j of them in the second for loop. However, I ran into the issue of not being able to choose from a list ("list objects are unhashable" error) with this code (I basically copied the for k in random.sample bit from an example I found on here):

for j in range(0,randomLevel):

    ptj = [xLength*random.random(),yLength*random.random(),zLength*random.random()]
    crvPtArray.append(ptj)

for i in range(0,crvNumber):

    crvPtArray2 = (crvPtArray[k] for k in random.sample(xrange(0,len(crvPtArray)), numRandPts))
    curves = rs.AddCurve (crvPtArray2, degree = numRandPts)
    crvArray.append(curves)

From here, I somehow stumbled onto the topics of dictionaries in python and tested these out but couldn't find a way to just print the list of "definitions" stored in the dictionary and not the "words" attached by a colon to the "definitions". And then I realized that even if I could do that the randomization would still be occurring in the dictionary for loop, not the curve-generating for loop (unless I'm wrong and it would randomize in the second for loop). So then I tried to generate blank points with pt[j] = [] and tried to somehow get the randomization to happen in the curve-generating for loop. Unfortunately, I didn't really have any success as I was completely out of my depth by this point and just trying to make up code that seemed like it might work.

Is there some way to create X blank variables or points and then retroactively define/randomize them? Is there a way to get my set and random sample idea to work? Is there some kind of crazy nested coding or something that might work for this? Any insight y'all could offer would be much appreciated.

(I there are any really really really dumb errors in my code they are probably transcriptional, but if there are any really dumb errors those might be mine ;))




Aucun commentaire:

Enregistrer un commentaire