Randomness Utilities #300

Parent #72Owner #361Flags readSource hellcore/hellcore.db

Aliases: Randomness Utilities, ru, random_utils

1 verbs · 11 properties · 0 children

Verbs

VerbSpecFlagsDefinerLines
weighted_randomthis none thisrxd#30012

Properties

PropertyDefinerFlagsOwnerValue
help_msg#72rc#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#1rc#361{"Randomness Utilities", "ru", "random_utils"}
description#1rc#361<clear>
object_size#1r#29{1347, 1298434423}
hidden_verbs#1rc#361<clear>
phelp_msg#1rc#361<clear>
weight#1rc#361<clear>
owner_verbs#1rc#361<clear>
plural_name#1rc#361<clear>
client_image#1rc#361<clear>
listening#1rc#361<clear>

Ancestry

Ancestors (nearest first): #72 Generic Utilities Package#1 root

Children: none

Call graph

calls n300_0 #300:weighted_random n47_6 #47:find_insert n300_0->n47_6

Source

weighted_random

Spec this none thisFlags rxdOwner #361Definer #300

Referenced by

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];