Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Too tired to think, Math Gurus read! :)

Agent Case
Second Life Resident
Join date: 27 Nov 2004
Posts: 18
03-02-2005 23:07
Hello im scripting a object that requires it to find a random item name from a strided list, based on odds from the attached number in the stride. The strided list contains two things in each group, 1) a name 2) a number .
The higher the number is the better chance the name should have at being selected randomly.

So, if we have a few varialbles in the stride like
DALIST = ["tree",10,"dog",5,"blah",6];

"tree" should have a chance at being selected with the odds of 10 in 21 (all numbers added up).
dog odds are 5 in 21, and blah odds are 6 in 21 ..

anyways, I know I could just create a new list and add each name to it as many times as NUMBER is.. then find a random name from that list .. but that could take A LOOTTT of memory, adding tree to a list say 250 times adds up pretty quick... Im just wondering if theres some sort of function/algorithm I could use to do what im trying to accomplish WITHOUT having to create a new list and using the variable groups I can get .. which are name and number.

I currently have this code in place temp, which finds a random name from the strided list (looking at only the even numbers in the list, those being the names.)
CODE

integer GetEvenNumberFromlist() {
integer random = (integer)(randBetween(0,llGetListLength(DALIST)));
while((integer)(random & 1) != 0) {
random = (integer)(randBetween(0,llGetListLength(DALIST)));
}
return random;
}

function_later_in_the_script_:P() {
integer RandEvenNumber = GetEvenNumberFromlist();
string pickedname = llList2String(DALIST, RandEvenNumber);
}

but the problem with this code is, it doesnt use the numbers at all to determine the winner, and all have the same odds. I need to somehow incorporate the numbers in this code to determine the winner how i described in above writings.
Getting the name and attached number from the strided list saved in a string and integer is not hard for me, so thats no problem.


hope I didn't confuse you too much, maybe someone can help.. its almost 2AM and im hoping someone will come up with a logical answer by morning so I don't have to come up with my own solution :)

Thanks.
Jonathan Shaftoe
... the titleless.
Join date: 11 Feb 2005
Posts: 44
03-03-2005 03:38
There's a fairly straightforward solution, though it does mean parsing through the list twice, unless you make all your odds actually be percentages and make sure they always add up to 100 (or some other known number).

First, loop through the list and add up each of your 'likelyhood' numbers (they're not really expressed as odds, so I'm avoiding calling them that), to get the total.

Second, pick a random number between 0 and this total number.

Third, initialise a 'running count' to 0, then loop through the list again adding up the 'likelyhood' numbers as you go. As soon as your 'running count' exceeds your random number, stop, and the previous item (not current) is the one you've selected.

Hope that makes sense, I can do a more pseudo-codey version if you need it.
Jonathan Shaftoe
... the titleless.
Join date: 11 Feb 2005
Posts: 44
03-03-2005 03:43
Oh, and for the record, you don't need the while loop in your 'get a random item from strided list.

Instead of getting a random number from 0 to listlength and looping until you get an even one, get a random number from 0 to listlength / 2 and then multiply by 2 to get your index. This can be extended to strided lists of other lengths, if your stride is 3, get a random number from 0 to listlength / 3 and then multiply by 3, for example (adding 1 or 2 if necessary to get the second or third items in the stride rather than the first).
Agent Case
Second Life Resident
Join date: 27 Nov 2004
Posts: 18
03-03-2005 04:20
ahhh that actually is making sense. I visualized it in my head , but hav'nt wrote any code yet. Ill let you know how I do once I go to sleep and wake up later today (its 7am now lol )... Thanks
Agent Case
Second Life Resident
Join date: 27 Nov 2004
Posts: 18
03-03-2005 05:38
could I get some code for step 3? Im don't know what you mean by running count.
Thanks
Lex Neva
wears dorky glasses
Join date: 27 Nov 2004
Posts: 1,361
03-03-2005 09:50
Just a slight addendum to Jonathan's algorithm that will make it easier to code. Do the first pass-through as he suggested, finding the sum of the odds numbers. Pick a random number between 0 and the sum minus one (use llFloor(llFrand(sum));). Now loop through the list, and each time subtract the odds number from the random number. If it's ever below zero, pick the current item. As long as your list isn't empty and doesn't have all 0 odds, it's guaranteed to go below zero. This way you don't have to figure out how to get at the previous item.
Catfart Grayson
Registered User
Join date: 16 May 2004
Posts: 264
03-03-2005 11:17
You would have to modify your odds slightly, but why not just use two random numbers to select the name?
_____________________
Cat