View Full Version : rand command

11 November 2012, 02:55 AM
Hi there,

I've written a script that generates images for scene files, however I have this line:

float $rando, $rando= rand(.15, .85);
setAttr ($plane_mat + ".color") -type double3 $rando $rando $rando;

Every time the script is called it launches a new instance of maya.exe (without UI) and $rando is generated....but it's always the same colour.

Obviously if you use the code in one instance of Maya, it works okay. Anyone know what to do ? How to make it random everytime maya.exe opens?

11 November 2012, 10:30 AM
computers can't actually generate real random numbers, they can only choose pseudo-random numbers from a stream, or list. It seems that becasue your script is spawning a new instance of Maya in the same way each time, the random stream it's using is in the same state each time.
Open up the MEL docs for the rand command and read the info at the top which explains how streams work, how you can set them and other related commands like seed.
It may be that in your script you have to set the seed or stream to something "random" like the current time or date, etc..

incidentally, you know you'll only ever get grey colours using that code right? as you use the same value for R, G and B. I'm assuming this is intentional, but I thought it best to mention it anyway just in case. Additionally, you can create random vectors too which is good for random colours.

11 November 2012, 12:18 PM
Thanks Nathan for taking the time to reply to simple problems like this....I thought this might have been the case, but I didn't know how to go about it, now I do! :beer:

Yes, the intention is to get random grey backgrounds in my modelBank browser (like this:

Makes it easier for me and looks better for some reason.

CGTalk Moderation
11 November 2012, 12:18 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.