The search keyword must be a single word (no spaces).
Function RandomDistChoose
- Line
- 10516
- Location
- blizzard.j
- Constant?
- no
- Type
- function
- Arguments
- nothing
- Returns
- integer
| 10516 | function RandomDistChoose takes nothing returns integer |
| 10517 | local integer sum = 0 |
| 10518 | local integer chance = 0 |
| 10519 | local integer index |
| 10520 | local integer foundID = -1 |
| 10521 | local boolean done |
| 10522 | |
| 10523 | // No items?
|
| 10524 | if (bj_randDistCount == 0) then |
| 10525 | return -1 |
| 10526 | endif
|
| 10527 | |
| 10528 | // Find sum of all chances
|
| 10529 | set index = 0 |
| 10530 | loop
|
| 10531 | set sum = sum + bj_randDistChance[index] |
| 10532 | |
| 10533 | set index = index + 1 |
| 10534 | exitwhen index == bj_randDistCount |
| 10535 | endloop
|
| 10536 | |
| 10537 | // Choose random number within the total range
|
| 10538 | set chance = GetRandomInt(1, sum) |
| 10539 | |
| 10540 | // Find ID which corresponds to this chance
|
| 10541 | set index = 0 |
| 10542 | set sum = 0 |
| 10543 | set done = false |
| 10544 | loop
|
| 10545 | set sum = sum + bj_randDistChance[index] |
| 10546 | |
| 10547 | if (chance <= sum) then |
| 10548 | set foundID = bj_randDistID[index] |
| 10549 | set done = true |
| 10550 | endif
|
| 10551 | |
| 10552 | set index = index + 1 |
| 10553 | if (index == bj_randDistCount) then |
| 10554 | set done = true |
| 10555 | endif
|
| 10556 | |
| 10557 | exitwhen done == true |
| 10558 | endloop
|
| 10559 | |
| 10560 | return foundID
|
| 10561 | endfunction
|