| 10-07-2008, 02:25 AM | #1 |
Hi. Got a problem I can't think though myself. Per-say I had a bunch of different percents (1%-100%) which are related to a chance to make different destructables. How would I go about, based on the percents, to pick one destructable to make. Just a example for clarification (I hope): JASS:function Something takes nothing returns nothing local integer i = 0 //Just a counter local integer array destructables //Holds the destructable id's local real array chance //Chance to make the above destructable set destructables[1] = 'ATtc' //A tree set chance[1] = 0.50 //50% chance set destructables[2] = 'ATtr' //Another tree set chance[2] = 0.90 //90% chance set destructables[3] = 'B004' //A bush set chance[3] = 0.20 //20% chance loop set i = i + 1 call CreateDestructable(destructables[pick destructable based on chance], 0, 0, 0, 1, 1) exitwhen i == 100 endloop endfunction |
| 10-07-2008, 03:49 AM | #2 |
Could've been set more generic, and much simpler with vJass, but I did not want to stray a little much from your code. JASS:function Something takes nothing returns nothing local integer i = 0 //Just a counter local integer j = 0 local integer array destructables //Holds the destructable id's local real array chance //Chance to make the above destructable local real pick = 0.0 set destructables[1] = 'ATtc' //A tree set chance[1] = 0.50 //50% chance set destructables[2] = 'ATtr' //Another tree set chance[2] = 0.90 //90% chance set destructables[3] = 'B004' //A bush set chance[3] = 0.20 //20% chance loop set i = i + 1 set pick = RandomReal(0,1.0) //whatever the random real function is loop set j = j+1 exitwhen (j > arraysize) exitwhen (pick <=chance[j])//for this your chances to be picked have to be sorted in the array eg: chance[1] = 0.20, chance[2] = 0.50, chance[3] = 0.90, etc endloop call CreateDestructable(destructables[j], 0, 0, 0, 1, 1) exitwhen i == 100 endloop endfunction |
| 10-07-2008, 04:14 AM | #3 |
Going vJASS is fine. I believe I can do it now. if you have another way in vJASS go ahead and post it please. |
| 10-07-2008, 12:53 PM | #4 |
That's all wrong. First of all, if you want actual percent, then your chances need to add up to 100% or 1.0. In your case, they don't, so 0.9 isn't really 90%, it's 0.9/(0.2+0.5+0.9)=0.5625=56.25%. Furthermore, what Alevice posted is incorrect. To get a proper distribution of random picks you need to pick a random number between 0 and the sum of all chances, then loop through the array and compare the picked real to the sum of all chances looped through so far and exit the loop once the picked real is smaller. Well, either that or the opposite, which doesn't require you to know the sum in advance: JASS:scope RandomDestructable initializer init globals private integer array destructables private real array chance private integer arraysize = 0 endglobals private function AddDestToList takes integer dest, real ch returns nothing set destructables[arraysize] = dest set chance[arraysize] = ch set arraysize=arraysize+1 endfunction private function init takes nothing returns nothing call AddDestToList('ATtc', 0.5) //A tree call AddDestToList('ATtr', 0.9) //Another tree call AddDestToList( 'B004', 0.2) //A bush endfunction function GetRandomDestType takes nothing returns integer local real total = 0.0 local integer i = 0 local integer picked = 0 loop exitwhen i==arraysize set total = total + chance[i] if GetRandomReal(0.0, total) <= chance[i] then set picked=destructables[i] endif set i=i+1 //fixed, it should work now endloop return picked endfunction endscope |
| 10-07-2008, 04:50 PM | #5 |
Ok I've modified it and it seems to be working. Thanks. Btw, I think you forgot to add: JASS:set i = i + 1 |
| 10-07-2008, 07:31 PM | #6 |
Hmm, right, I always forget that. I hope you added it right before the endloop. |
| 10-08-2008, 02:32 PM | #7 | |
Quote:
Damn, you are right. Mine would only pick a unit that fell on a given range. Damn math. |
