kutya 0 Posted July 10, 2006 I want to use random selection from an array. Let's have: obj = [obj1, obj2, obj3, obj4] I saw in a script the next: <table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">objs select (random ((count objs) - 1))} As I know random returns real. It's not clear to me how can you select with a real number? Only have two ideas: the random number is truncated, or rounded. Does someone know which is? I think trunc, but not sure. Share this post Link to post Share on other sites
ProfTournesol 956 Posted July 10, 2006 Well, an easy way to round a random number is using "mod" command : <table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE"> _r = random 2 ; this return a real value between 0 and 2 _t = _r - (_r mod 1) ; this return a rounded value of the previous real number "_r" Share this post Link to post Share on other sites
Metal Heart 0 Posted July 10, 2006 Well, an easy way to round a random number is using "mod" command :<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE"> _r = random 2 ; this return a real value between 0 and 2 _t = _r - (_r mod 1) ; this return a rounded value of the previous real number "_r" Odds for getting 2 out of that is one (2.0) against two million (0.0-1.999999). Share this post Link to post Share on other sites
kutya 0 Posted July 10, 2006 Yeah, I noticed that, that's why I thought it's better to use trunc. But didn't test it yet. In that case, the odds would be the same. Share this post Link to post Share on other sites
Chris Death 0 Posted July 10, 2006 errm - forget what i wrote - i can't test it right now because i'm at work and can't test anything to proove it. And i mixed up something during reading the post. ~S~ CD Share this post Link to post Share on other sites
kutya 0 Posted July 11, 2006 After implementing, the command in the first post really works! It's made by Silola. He's really a genius. This way, if you define an array, you can in a single line choose a random element. If you need some random trucks in a convoy, define an array with truck model names, and a couple of times call createvehicle with the above mentioned code, and you've got a convoy with random trucks. Really simplifies the lengthy case structure. Share this post Link to post Share on other sites
Metal Heart 0 Posted July 12, 2006 Actually it's flawed. The first and last array element have only half the chance of happening than the ones in the middle. <table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">arr = [0,1,2,3]; arr select (random ((count arr) - 1)); // random 3 in this case Here are the ranges: element 0 = 0.0 - 0.499999 element 1 = 0.5 - 1.499999 element 2 = 1.5 - 2.499999 element 3 = 2.5 - 3.0 Share this post Link to post Share on other sites
kutya 0 Posted July 12, 2006 I thought about this, but was too busy to test it. I didn't know, is the select command truncating or rounding if it has a real as a parameter. I would like if it was trunc, but you proved otherwise. With trunc it would work correctly I think. But surely, thanks for the info! Share this post Link to post Share on other sites
cleanrock 0 Posted July 12, 2006 i do it this way : _list = [0, 1, 2, 3] _entry = _list select ((random (count _list)) - 0.5) this will select a random entry in the list and all entries will have equal probability of being selected Share this post Link to post Share on other sites
Chris Death 0 Posted July 12, 2006 Well, but then the first element doesn't have half of the chance. If you subtract 1 from 0 it's -1 Maybe the chances are even if you subtract only 0.5 from the whole array count. ((random 4) - 0.5). Only what i dunno now is if it would produce a minus number in this case anyway, but if not you could do it with one line afterwards creating the random. ~S~ CD Share this post Link to post Share on other sites
Metal Heart 0 Posted July 12, 2006 Quote[/b] ]_list = [0, 1, 2, 3]_entry = _list select ((random (count _list)) - 0.5) this will select a random entry in the list and all entries will have equal probability of being selected It returns null if random hits the maximum value. A very small chance though but it could happen Quote[/b] ]Maybe the chances are even if you subtract only 0.5 fromthe whole array count. ((random 4) - 0.5). That's exactly what it does, see the parenthesis. Share this post Link to post Share on other sites
cleanrock 0 Posted July 14, 2006 It returns null if random hits the maximum value. A very small chance though but it could happen i tried it and this is the way bis do it, if u make a script which test this i am pretty sure it will never be the max value Share this post Link to post Share on other sites
Metal Heart 0 Posted July 15, 2006 I guess I have to agree if BIS uses such script too. However, here's what the command reference says: Quote[/b] ]random xOperand types: x: Number Type of returned value: Number Description: random real value from 0 to x Example: random 1 Share this post Link to post Share on other sites