Skip to content

Separate seed and state in PolyMath random number generators #337

@olekscode

Description

@olekscode

Several random number generators in PolyMath currently use a single instance variable, seed, both as the initial seed provided by the user and as the internal state of the generator.

The root of the issue is that PMPseudoRandomNumberGenerator defines only a single slot:

Object << #PMPseudoRandomNumberGenerator
    slots: { #seed };
    package: 'Math-RandomNumbers'

As a consequence, subclasses update seed while generating random numbers:

PMFishmanMooreRandomGenerator >> next
    ^ (seed := 742938285 * seed \\ 2147483647) / 2147483647.0
PMParkMillerMinimumRandomGenerator >> nextFloat
    seed := self peekSeed.
    ^ seed / 16r7FFFFFFF asFloat

This means that the value returned by seed changes every time a random number is generated:

rand := PMParkMillerMinimumRandomGenerator seed: 42.

rand seed.   "42"
rand next.
rand seed.   "1138636912"

While this behavior reflects the internal implementation of the generators, it is somewhat surprising from an API perspective. Users often expect seed to represent the value used to initialize the generator and therefore remain unchanged throughout its lifetime.

The Random class of Pharo already makes this distinction explicit:

Object << #Random
    slots: { #state . #seed };
    tag: 'Base';
    package: 'Random-Core'
rand := Random seed: 42.

rand seed.   "42"
rand next.
rand seed.   "42"

Proposal

Introduce a state slot in PMPseudoRandomNumberGenerator and use it as the mutable internal state of the generator, while preserving seed as the original initialization value. Change the random number generation algorithms to operate on state, while keeping seed unchanged.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions