PDA

View Full Version : Random number from a list

 ossosso01-19-2007, 02:44 PMHi everyone! I used this kind of Creation Expression: particleShape1.goalU=rand (0,1); to make my particles start their flow along a surface from a random startpoint; to these particles Instance objects are linked, and some particles born too much near to others, in this way the instance objcets touch themesleves and I don't want this. I tried to use a radial field applied to each particles, but this change the direction of the velocity vector for each particles, and therefore also the direction of the objects aim vector. So I'm here to ask you if there's a function or any other way that give me as output a random value inclued by a range of predefined values. For example I create an array with thisi value: [0.1,0.2,0.3,0.4,0.5... 1] and the function takes a random value from these; in this way particles born more distant each other. Thank you very much for any help.
01-19-2007, 04:02 PM
you can do this:

float \$randList[] ={0.1, 0.2, 0.3 ,0.4};
float \$randNum = \$randList[(int) floor (rand(0, (size(\$randList) - 1) ) + 0.5 )];

rand(0, (size(\$randList) - 1) ) gets a number between 0 and the last item index in the array

floor( above +0.5) ensures that the index gets rounded to the nearest integer

Hope that helps.

cheers

ossosso
01-19-2007, 04:52 PM
Wow! Thanks friend, it works :)

but I can't found into the documentation info about the "floor()" function, so I haven't understood the part "(int) floor (...)" what does it mean?

01-19-2007, 05:10 PM
float \$randList[] ={0.1, 0.2, 0.3 ,0.4};

This expression defines an array that contains 4 elements. Indexes start at 0. So the indexes here are 0, 1, 2, 3

we now need an expression to pick one of those indexes randomly. Indexes can only be integers so we need to make sure of that.

So we'll pick a random number between the first index and last index, in our case between 0 and 3. The returned number is a float, so we need to convert it to the nearest Integer with the floor(x + 0.5) expression. (if you only use floor(x), 2.3 will give u 2, 2.9 will give you 2 as well). Ceil() works the opposite way: 2.3 will give 3, 2.9 will give 3 as well. Using floor(x +0.5) or ceil( x - 0.5) gives you the nearest number.

Below is cleaner code that does the same as the one i gave you.

float \$randList[] ={0.1, 0.2, 0.3 ,0.4};

int \$firstIndex = 0; // by default first index is 0
int \$lastIndex = size(\$randList) -1; // since it starts at 0 we need to subtract 1 to get last Index

float \$randIndexFloat = rand(\$firstIndex, \$lastIndex); // returns random numb between 0 and 3
int \$randIndexInt = floor(\$randIndexFloat + 0.5); // converts 2.3 to 2 for ex, or 2.7 to 3 (nearest int number)

\$randNum = \$randList[\$randIndexInt]; // gets that number from the array

ossosso
01-19-2007, 05:46 PM
Thanks, now it's all clear :), I didn't know floor() and ceil() functions, and I think I'll use them more.
But.... I've just the last request :), if you can... otherwise don't worry, you have just done enough.

Your expression work correctly but could happens that the \$randNum for two or three or four frames it's always the same number, this means that for some sequential frames particles born on the same position, one behind and near the other... and again this means that instance objects are too near and they overlap.
It's possible to update your expression in the way that \$randNum could not be the same value for a range of, for example, 30 frames ?

If it's not no problem :).

Thanks again.

P.S.
On the base of your expression I wrote this

float \$randList[] ={0.2, 0.32 ,0.44,0.56,0.68,0.8};
float \$randNumb = \$randList[(int) floor (rand(0, (size(\$randList) - 1) ) + 0.5 )];
float \$randLista[] ={-0.12,0.12};
float \$randNumbe = \$randLista[(int) floor (rand(0, (size(\$randLista) - 1) ) + 0.5 )];

particleShape1.goalV = \$randNumb + \$randNumbe;

But it seems not to be enough in the sense that the second particle that borns will not be on the same position of the previous, but the third could :), and the distance is yet not enough.

CGTalk Moderation
01-19-2007, 05:46 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.