ZBrushCentral

Random Number Generator Issue

I’m writing a script that requires me to select a random subtool from a pre-defined set (based on the Index assignments, up to 4). The actual subtool index is stored in a variable array: sToolID(0-3). The number of user assigned subtools is stored in stAssigned.

I start to run into problems however with the random number generator. If I put it in a loop, I can see that the picks random values, but it gets sequentially smaller. So in my script I only end up picking stID 1 or 2 once or twice, then I’m always randomly picking stID 0 there-after.

Anyone else have issues with the random number generator? Should I be resetting the random generator after every random result?


[VarSet, stIndex, [IGet, "ZPlugin:Toolbox:ST Index"]]
[SubToolSelect, sToolID([Var,stIndex])]


[Randomize]
    [Loop, 20,
        [VarSet, randomID, Rand([Var,stAssigned])] ///---randomly pick a subtool from the pre-defined user set
        [note, [StrMerge, "random: ", #randomID],,0.5]
[VarSet, randomID, Int(#randomID)]
[note, [StrMerge, "random: ", #randomID],,0.5]
    ]

Also… I just re-made this thread in the help forum, I realized that I accidentally originally made it in the utilities forum. I apologize! Can a moderator delete the one in the utilities forum?

Did you try using IRAND instead of RAND? It’s a while since I’ve used RAND but I’ll try and take a look tomorrow. I don’t think putting Randomize in the loop would work.

Thanks Marcus. I did try IRAND at first, that is where i first noticed the issue where it pick 2 or 1, and then only 0 there-after.

So switched to RAND hoping for a different behavior, but it game me random numbers in sequence as well.

OK, I think I have found the problem. Try using [Val] instead of [Var]:


[VarSet, randomID, Rand([Val,stAssigned])]

Not sure why that makes a difference in this case but it does. Also, adding 0.5 to the random value will probably give a more even distribution:

[VarSet, randomID, INT(#randomID+0.5)]

HTH,

That seems to do it! Thanks again Marcus! and thanks for the .5 / probability tip. I also tried using IRAND again with the Val, command it works as I would expect.