Paste #2613: Weighted Random Choice

Date: 2014/03/18 18:36:54 UTC-07:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


################################################################################
#                                                                              #
#                  W e i g h t e d   R a n d o m   C h o i c e                 #
#                                                                              #
#                                                                              #
#  This enables you to have a percent chance that something will happen. Good  #
#  for things such as NPC interactions and/or for daily loot. Easily set some  #
#  things as rare and others as common. Check out the example usage for ideas. #
#                                                                              #
################################################################################

'WeightedRandomChoice':
#
# Choose a random number from 1 to 100 and return which category the number is
# within.
#
  type: task
  script:
    - define list <npc.flag[WeightedRandomChoice].as_list>
    - define listSize <def[list].size>
    - define number <util.random.int[1].to[100]>
    - define counter 0
    - foreach <def[list].as_list> {
      - if <def[counter].is[OR_LESS].than[%listSize%]> {
        - define counter <math:%counter%+1>
        - define entry_<def[counter].as_int> <def[list].get[<def[counter].as_int>]>
        - define percentChance <def[entry_<def[counter].as_int>].split[/].get[1]>
        - define returnValue <def[entry_<def[counter].as_int>].split[/].get[2]>
        - define oldValue '<def[oldValue] || 0>'
        - define lowValue '<math:%oldValue%+1>'
        - define highValue '<math:%oldValue%+%percentChance%>'
        - define oldValue '<def[highValue]>'

        - if <proc[isbetween].context[%number%|%lowValue%|%highValue%]> {
          - ^flag npc WeightedRandomChoice:<def[returnValue].as_int>
          }
        }
      }
    - define result <npc.flag[WeightedRandomChoice]>


#'Example Weighted Random Choice Usage':
#
# Run a random thing based on weight assignment
# - flag npc WeightedRandomChoice:percentChance/returnValue|...
#
# Make sure the sum of all the options is equal to 100.
#
#  type: task
#  script:
#    - flag npc WeightedRandomChoice:5/1|20/2|25/3|35/4|15/5
#    - inject s@WeightedRandomChoice instantly
#
#    - if <def[outcome].is[==].to[1]> {
#      - chat "5% Chance"
#      - run s@SomeRareTask
#      }
#      else if <def[outcome].is[==].to[2]> {
#      - chat "20% Chance"
#      - run s@SomeLessCommonTask
#      }
#      else if <def[outcome].is[==].to[3]> {
#      - chat "25% Chance"
#      - run s@SomeCommonTask
#      }
#      else if <def[outcome].is[==].to[4]> {
#      - chat "35% Chance"
#      - run s@SomeMoreCommonTask
#      }
#      else if <def[outcome].is[==].to[5]> {
#      - chat "15% Chance"
#      - run s@SomeKindaRareTask
#      }
#      else {
#      - chat "something went horribly wrong"
#      }
#      - flag npc WeightedRandomChoice:!