Skip to content

Main content

Random Reservoir Stream Sample Generator

Apply Algorithm R to ordered supplied records, returning portable reservoir state and update decisions with deterministic seeded chunk continuation.

Loading tool…

Description

Apply Algorithm R to ordered supplied records, returning portable reservoir state and update decisions with deterministic seeded chunk continuation.

Sampling selects from a supplied population under rules for replacement, grouping, or eligibility. Draw probabilities, inclusion probabilities, and repeat counts describe different aspects of selection; a reproducible sample is not automatically representative of a real population.

Apply Algorithm R to ordered supplied records, returning portable reservoir state and update decisions with deterministic seeded chunk continuation.

When to use this tool

Use this generator to create reproducible reservoir stream sample cases from the supplied model or bank. Inspect the returned evidence before using cases in a test or exercise.

How to use the tool and how it works

  1. Supply capacity, chunk, state in the field shapes below. Set seed when you need an explicit replay.
  2. Run the tool and correct any input error. For generated controls, distinguish a successful fixture containing an invalid case from failure to generate the fixture.
  3. Read state, updates, inclusionProbability. Use the conventions below and keep answer-bearing fields separate when presenting an exercise.

Apply Algorithm R: fill the first capacity slots, then for stream index i draw j from zero through i and replace slot j only when j is below capacity. Return portable state and each fill, replace, or skip decision. Inclusion probability is min(capacity,seen)/seen, or zero when empty.1

Input fieldMeaning and unitsAccepted input
capacityCapacityRequired. Safe integer; minimum 0; maximum 512.
chunkChunkRequired. Array of Object; no unknown keys.; minimum items 0; maximum items 2048.
chunk[].idLiteral identifier used to match records, independently of their display labels.Required. Text; minimum UTF-16 length 1; maximum UTF-16 length 128.
chunk[].valuesValuesRequired. Array of Text; maximum UTF-16 length 4096.; minimum items 0; maximum items 32.
stateStateOptional. Object; no unknown keys.
state.capacityCapacityRequired. Safe integer; minimum 0; maximum 512.
state.seenSeenRequired. Safe integer; minimum 0; maximum 100000.
state.reservoirReservoirRequired. Array of Object; no unknown keys.; minimum items 0; maximum items 512.
state.reservoir[].streamIndexStream IndexRequired. Safe integer; minimum 0.
state.reservoir[].recordRecordRequired. Object; no unknown keys.
state.reservoir[].record.idLiteral identifier used to match records, independently of their display labels.Required. Text; minimum UTF-16 length 1; maximum UTF-16 length 128.
state.reservoir[].record.valuesValuesRequired. Array of Text; maximum UTF-16 length 4096.; minimum items 0; maximum items 32.
state.randomnessReplay metadata: source is seed or context; seed is the normalized 32-bit seed or null for a context source.Required. Object; no unknown keys.
state.randomness.seedOptional numeric replay seed; normalization and precedence are explained below.Required. Safe integer. or Null.
state.randomness.sourceSourceRequired. Text; one of "context", "seed".
seedOptional numeric seed; takes precedence over context randomness. Without either, use the fixed tool.io default seed.Optional. Safe integer.
Output fieldInterpretationReturned shape
stateStateRequired. Object; no unknown keys.
state.capacityCapacityRequired. Safe integer; minimum 0; maximum 512.
state.seenSeenRequired. Safe integer; minimum 0; maximum 100000.
state.reservoirReservoirRequired. Array of Object; no unknown keys.; minimum items 0; maximum items 512.
state.reservoir[].streamIndexStream IndexRequired. Safe integer; minimum 0.
state.reservoir[].recordRecordRequired. Object; no unknown keys.
state.reservoir[].record.idLiteral identifier used to match records, independently of their display labels.Required. Text; minimum UTF-16 length 1; maximum UTF-16 length 128.
state.reservoir[].record.valuesValuesRequired. Array of Text; maximum UTF-16 length 4096.; minimum items 0; maximum items 32.
state.randomnessReplay metadata: source is seed or context; seed is the normalized 32-bit seed or null for a context source.Required. Object; no unknown keys.
state.randomness.seedOptional numeric replay seed; normalization and precedence are explained below.Required. Safe integer. or Null.
state.randomness.sourceSourceRequired. Text; one of "context", "seed".
updatesUpdatesRequired. Array of Object; no unknown keys.; minimum items 0; maximum items 2048.
updates[].streamIndexStream IndexRequired. Safe integer; minimum 0.
updates[].decisionDecisionRequired. Object; no unknown keys.
updates[].decision.drawDrawRequired. Safe integer; minimum 0. or Null.
updates[].decision.slotSlotRequired. Safe integer; minimum 0. or Null.
updates[].decision.actionActionRequired. Text; one of "fill", "replace", "skip".
inclusionProbabilityInclusion ProbabilityRequired. Finite number; minimum 0; maximum 1.
randomnessReplay metadata: source is seed or context; seed is the normalized 32-bit seed or null for a context source.Required. Object; no unknown keys.
randomness.seedOptional numeric replay seed; normalization and precedence are explained below.Required. Safe integer. or Null.
randomness.sourceSourceRequired. Text; one of "context", "seed".

An explicit safe-integer seed takes precedence over injected context randomness and is normalized to an unsigned 32-bit integer. With no seed and no context source, replay uses the fixed tool.io default seed. A context source must return finite values from zero inclusive to one exclusive; its advancing state must be retained separately. randomness records the source and normalized seed, or null for context. These generators are not cryptographic random sources.

json
{
  "input": {
    "capacity": 2,
    "chunk": [
      {
        "id": "A",
        "values": [
          "oak"
        ]
      },
      {
        "id": "B",
        "values": [
          "pine"
        ]
      },
      {
        "id": "C",
        "values": [
          "elm"
        ]
      },
      {
        "id": "D",
        "values": [
          "ash"
        ]
      }
    ],
    "seed": 42
  },
  "output": {
    "state": {
      "capacity": 2,
      "seen": 4,
      "reservoir": [
        {
          "streamIndex": 0,
          "record": {
            "id": "A",
            "values": [
              "oak"
            ]
          }
        },
        {
          "streamIndex": 3,
          "record": {
            "id": "D",
            "values": [
              "ash"
            ]
          }
        }
      ],
      "randomness": {
        "seed": 42,
        "source": "seed"
      }
    },
    "updates": [
      {
        "streamIndex": 0,
        "decision": {
          "draw": null,
          "slot": 0,
          "action": "fill"
        }
      },
      {
        "streamIndex": 1,
        "decision": {
          "draw": null,
          "slot": 1,
          "action": "fill"
        }
      },
      {
        "streamIndex": 2,
        "decision": {
          "draw": 1,
          "slot": 1,
          "action": "replace"
        }
      },
      {
        "streamIndex": 3,
        "decision": {
          "draw": 1,
          "slot": 1,
          "action": "replace"
        }
      }
    ],
    "inclusionProbability": 0.5,
    "randomness": {
      "seed": 42,
      "source": "seed"
    }
  }
}

Limitations and assumptions

  • Capacity zero skips without draws. Continuation must preserve capacity and valid unique stream indexes. Seeded continuation restores the original normalized seed and skips prior post-fill draws; context continuation needs the same advancing random source. Cumulative seen is at most 100,000.
  • A replay requires the same complete input and implementation. Randomized construction does not guarantee uniform coverage of every feasible output; finite samples can repeat unless a distinct-selection rule is explicitly stated.
  • The field tables state structural bounds; cross-field constraints and bounded search or output budgets also apply. Unknown keys are rejected where the object contract disallows them. Input errors are distinct from null fields and deliberate invalid controls in a successful output.

Alternative or Complementary methods

For a supplied example, inspect the returned construction or delegated check directly. To evaluate a consumer, also test cases chosen for its own boundaries; a generated finite corpus does not establish exhaustive coverage.

References

  1. Tool behavior and example: Random Reservoir Stream Sample Generator — tool.io

Similar or alternative tools