Intro to Python Scripting: 09 Random Numbers 01

Software: 
Tag: 
Video Duration: 
8 minutes
Author: 
Zach Downey

In this tutorial we introduce python's Random() number class to generate pseudo-random numbers. We take a look at three different methods for creating random numbers: random.random(), random.randint(), and random.uniform(). We use these methods to create a series of 10,000 randomly posistioned and colored points within a given space.

#Random Numbers
 
import rhinoscriptsyntax as rs
import random
 
#prints a random floating point number from 0.0 to 1.0
print (random.random())
#prints a random integer from 0 to 100
print (random.randint(0,100))
#prints a random floating point number from 0.0 to 100.0
print (random.uniform(0,100))
 
 
for i in range(0,10000):
    x = random.uniform(0,100)
    y = random.uniform(0,100)
    z = random.uniform(0,100)
 
    r = random.randint(0,255)
    g = random.randint(0,255)
    b = random.randint(0,255)
    color = [r,g,b]
    pt = rs.AddPoint(x,y,z)
    rs.ObjectColor(pt, color)

Rating

Please rate this tutorial below. Thanks.

5
Average: 4.9 (7 votes)

Want to Contribute?

Want to be an author? Drop us a line here we'd love to have you.

Already have a video you'd like to post? Send us a link and we'll get you going.

:)