StoryRandom.h header

#include <ew/app/branching/StoryRandom.h>

Namespace ew::app::branching

Functions

std::optional< std::size_t > ew::app::branching::chooseByWeight(const std::vector< int > &weights, std::uint64_t roll)

The index roll selects from weights, each weight taking its own share of the total.

A weight of zero (or less) can never be selected – which is how an author disables a branch without deleting it, and is reported by the validator because it is also what a mistyped weight looks like. Returns nullopt when weights is empty or every weight is zero, since there is then nothing that could be chosen.

std::uint64_t ew::app::branching::storyRandom(std::uint64_t seed, ew::core::foundation::BranchNodeId nodeId, std::size_t visitIndex)

The roll for nodeId on the reader's visitIndex'th arrival, in a run seeded with seed.

A function of its arguments, not a generator that advances. Everything chancy in a story – which take of a line is said, which way out of a Random node is taken – asks this, so a playthrough replays exactly: stepping back and forward shows what the reader saw, and "on the third visit it went left" is a bug report somebody else can reproduce. A generator would make the answer depend on how many other chancy things had happened first, which is the property that makes a random bug impossible to chase.

SplitMix64's finalizer over the pair. Not a cryptographic hash and not meant to be one: what it has to do is scatter, so that consecutive visits do not walk the same way.