;

Pymel Blog Rss Feed

Friday, May 18, 2012


Prior to starting any of these lessons be sure to import the PyMEL library. See here for why and how.



When we think about programming we generally think about things order and neatness. Things like chaos don't normally come to mind. That is unless it's 3:00 am and you're desperately trying to get a tough script done for a 9:00 deadline. Then it's all kinds of chaos!

 

But every now and then, you do really need to introduce some bedlam. Python's random number generator will provide just the right bit of craziness for your code. And before we go further, I should note that Python doesn't really generate random numbers. I mean nothing natural is really random. Think of it more as pseudo-random, meaning it has the appearance of being random. There is a cryptic algorithm that Python uses to generate the numbers. But from a human perspective, it can be considered random. So if you're disappointed that random numbers generated by computers aren't really random, don't worry because Python's pseudo-random works great for what we need.

 

In order to use Python's random number generator, we first need to import the random library. The is easy as typing the following:

import random 

Now we have access the entire random library. Let's try to generate some random numbers:

 

Now the functions section contains a host of mathematical functions. Most of them look like throwbacks from high school geometry. Let's see them in action.

print random.random() #prints 0.873402310468
print random.uniform(0,10) #prints 7.4621945681
print random.randint(0,10) #prints 4

You’ll probably get different values than I got.

 

random.random( ) returns a decimal value between 0 and 1

random.uniform(x, y) returns a decimal value between x and y (including x and y)

random.randint(x, y) returns an integer value between x and y (including x and y)

 

Python's random library has a bunch of other generators, but these are the three that you'll probably use the most. And now armed with this new found knowledge, we can do the following:

import random
objSel = selected() #store the selected objects
listSize = len(objSel) #store the length of the list
for i in range(0, listSize):
	curObj = objSel[i] #grab the current object from the list
	curLocator = spaceLocator()#create and store locator
	xVal = random.uniform(-50, 50) #generate random x value
	yVal = random.uniform(-50, 50) #generate random y value
	zVal = random.uniform(-50, 50) #generate random z value
	curObj.translate.set(xVal, yVal, zVal) #place the object

So for all selected objects, we generate 3 sets of random values and then we move the objects.

 

Now sometimes, we'd like to be able to reproduce a random sequence. Nothing is worse than when your supervisor likes some randomization that you did 3 hours ago and you have no way of replicating it. After all, they're called random numbers for a reason right? How on Earth do you reproduce random numbers? Wouldn't that make them not random? To solve this quandary, we'll do what's called seeding our random values. The act of seeding our random number generator will allow us to duplicate our random sequences.

 

Try the following:

random.seed(1)
print random.randint(0,10) #prints 1
print random.randint(0,10) #prints 9
print random.randint(0,10) #prints 8
print random.randint(0,10) #prints 2
print random.randint(0,10) #prints 5

Now try:

random.seed(2)
print random.randint(0,10) #prints 10
print random.randint(0,10) #prints 10
print random.randint(0,10) #prints 0
print random.randint(0,10) #prints 0
print random.randint(0,10) #prints 2

Now let's go back to the first one:

random.seed(1)
print random.randint(0,10) #prints 1
print random.randint(0,10) #prints 9
print random.randint(0,10) #prints 8
print random.randint(0,10) #prints 2
print random.randint(0,10) #prints 5

And the second one again:

random.seed(2)
print random.randint(0,10) #prints 10
print random.randint(0,10) #prints 10
print random.randint(0,10) #prints 0
print random.randint(0,10) #prints 0
print random.randint(0,10) #prints 2

I know what you’re thinking...I just copied and pasted. Well go ahead and try for yourself and then you’ll see that by specifying a seed number, we are able to generate a reproducible sequence of random numbers. If you ever find yourself in a position where you need to generate the same set of random numbers, be sure that you seed your randoms first.

 

To find out more about random numbers in Python, you can go here.

 

Generating random numbers for your scripts is easy. Now go! Create random things!

My comment system is powered by Disqus. And they require you to put in a name and an email. But I'd love to hear what you think. So if you want to comment anonymously, just put any name and test@test.com for your email.

blog comments powered by Disqus