FORUMS: list search recent posts

Array of random numbers that don't change each frame

COW Forums : Autodesk Maya

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
Edwin Bradford
Array of random numbers that don't change each frame
on Oct 7, 2011 at 3:52:30 pm

This was quite a headache. I was trying to create an array of random numbers that I could use to offset objects randomly via translation, rotation and time delay. However using... $myVariable = rand(lowNumber, highNumber); ...creates a new random number on each frame which was no use. I subsequently managed to create fixed random numbers for all frames using... float $myVariable = ((noise($i) + 1)); ...where $i is within a "for loop" counting upwards. However although $myVariable appeared to be truly random looking at the numbers, for some reason it was giving an unwanted non-random result in my animation.

Instead I came up with... float $myVariable[];
if (frame == 0) {
$myVariable[$i] = rand(0,20);
}
...and it works. However it feels a bit knocked together :) Is there a "proper" way to create a range of random numbers with MEL's rand()function that are fixed for all frames?


Return to posts index

Steve Sayer
Re: Array of random numbers that don't change each frame
on Oct 11, 2011 at 1:31:30 pm

Yes, you need to use the 'seed' command to initialize the random number generator. Look it up in the docs; there should be some examples of use.

-Steve


Return to posts index

Edwin Bradford
Re: Array of random numbers that don't change each frame
on Oct 11, 2011 at 5:15:58 pm

Thanks, although that doesn't seem to be what this article says, in particular... "By setting the seed value to an arbitrary number, for instance, 10, the subsequent executions of the rand function return a repeating sequence of random numbers."

According to that it would still create a new random number on each frame but a repeatable one which is not what I needed. However I found an example elsewhere...
if (frame == 1)
seed(500);
Ball.translateX = rand(5);
Ignoring the seed in this example, because I'm not worried about repeatability, it shows the same "if (frame==1)" I used already so I guess I had the right approach anyway :)


Return to posts index


Steve Sayer
Re: Array of random numbers that don't change each frame
on Oct 11, 2011 at 5:44:39 pm

It seems I misunderstood what you were after. I assumed you wanted repeatability, hence the use of 'seed'.

If you don't need repeatability, but just want to randomize a bunch of objects once, why use an expression that executes every frame instead of a script that you run once, manually, in the script editor? A scriptNode might also be a possibility.

-Steve


Return to posts index

Edwin Bradford
Re: Array of random numbers that don't change each frame
on Oct 11, 2011 at 7:00:09 pm

I haven't looked at Script Nodes at all, sounds very promising, I'll do some googling and read up on them in the Maya Help. I have several scripts / expressions running but so far I've only used the expression editor, thanks for the suggestion.


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]