JSSM, a JavaScript state machine - the FSM for FSL
    Preparing search index...

    Function weighted_rand_select


    • Selects a single item from a weighted array of objects using cumulative probability. Each object in the array should have a numeric property indicating its relative weight (defaults to 'probability'). Objects missing the property are treated as weight 1.

      const opts = [
      { value: 'common', probability: 0.8 },
      { value: 'rare', probability: 0.2 }
      ];

      weighted_rand_select(opts); // most often { value: 'common', ... }

      Parameters

      • options: any[]

        Non-empty array of objects to choose from.

      • probability_property: string = 'probability'

        Name of the numeric weight property on each object. Defaults to 'probability'.

      • Optionalrng: JssmRng

        Optional random number generator () => number in [0, 1). Defaults to Math.random.

      Returns any

      One element from options, chosen by weighted random selection.

      If options is not a non-empty array of objects.