Lesson 4: Randomness…

Just a quick one today on random numbers.

It’s really easy to make random numbers in maxscript, there’s a built in random number function.

Random 1 100

This will return a random number between 1 and 100, and it will be an integer (whole number) as the first parameter we defined for our random function is 1 which is an integer.

If we were to use a float value instead then we would get a random float value returned, even though the second value is an integer.

random 1 100
--returns 58
random 1.0 100
--returns 42.3792

We can get a random point2 values and random point3 values, by setting our parameters to point2 and point 3 values, the numbers in the point2 and point3 value will be float
random [0,0] [100,100]
--returns [23.308,52.8506]
random [0,0,0] [100,100,100]
--returns [0.715056,35.2274,1.81763]

If we want to get a random colour it will also return random numbers, again float which will be rounded to their nearest whole numbers when used as a colour. Note the brackets, if you don’t have them then you’re effectively giving the random function 8 parameters where it expects 2, I tend to always enclose my colour values in brackets like this. (Also note the American spelling of colour, they spell it color)

random (color 0 0 0) (color 255 255 255)
--returns (color 59.735 61.6328 223.568)

We can have a bit more control of our randomness, say if we want a random red colour.

colour (random 0 255) 0 0
--returns (color 168 0 0)

Or if we want a random greyscale value…

r = random 0 255
--returns 67
(color r r r)
--return (color 67 67 67)

About davewortley

Somewhere between an artist and a programmer, I like technical things, but being creative. I love problem solving and coming up with elaborate solutions. This blog is where I shall share ideas, tips, tutorials and anything that amuses me.
This entry was posted in Lessons, MaxScript. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s