Randomness Utilities #300
Aliases: Randomness Utilities, ru, random_utils
1 verbs · 11 properties · 0 children
Verbs
| Verb | Spec | Flags | Definer | Lines |
|---|---|---|---|---|
weighted_random | this none this | rxd | #300 | 12 |
Properties
| Property | Definer | Flags | Owner | Value |
|---|---|---|---|---|
help_msg | #72 | rc | #361 | list of 9{"Randomness Utilties (aka $random_utils, $ru)", "--------------------------------------------", "", "this poor little utils package needs help, for now it only has one verb.", "", " :weighted_random({INT weight, ANY result}, {INT weight2, ANY result2}, ", " ... ) => ANY chosen result, based on provided weights.", "", ""} |
aliases | #1 | rc | #361 | {"Randomness Utilities", "ru", "random_utils"} |
description | #1 | rc | #361 | <clear> |
object_size | #1 | r | #29 | {1347, 1298434423} |
hidden_verbs | #1 | rc | #361 | <clear> |
phelp_msg | #1 | rc | #361 | <clear> |
weight | #1 | rc | #361 | <clear> |
owner_verbs | #1 | rc | #361 | <clear> |
plural_name | #1 | rc | #361 | <clear> |
client_image | #1 | rc | #361 | <clear> |
listening | #1 | rc | #361 | <clear> |
Ancestry
Ancestors (nearest first): #72 Generic Utilities Package → #1 root
Children: none
Call graph
Source
weighted_random
Referenced by
- #27:random line 16:
$ru:weighted_random
Source
1":weighted_random( {INT weight, ANY result}, {INT weight2, ANY result2}, ... ) => ANY chosen result, based on weights."; 2choices = args; 3cumulative_weights = {}; 4sum = 0; 5for choice in (choices) 6wt = choice[1]; 7sum = sum + wt; 8cumulative_weights = {@cumulative_weights, sum}; 9endfor 10n = random(sum) - 1; 11idx = $lu:find_insert(cumulative_weights, n); 12return choices[idx][2];