Feat/global rng - #375
Merged
Merged
Conversation
…e of candidates to indices (coerce to str)
graceg571
force-pushed
the
feat/global-rng
branch
from
August 3, 2026 18:08
b98d2bf to
b989730
Compare
…ansfer accept Candidate
graceg571
marked this pull request as ready for review
August 3, 2026 20:36
…, update tests, update docstrings
…y veto, sort candidates cast per profile, add tests
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses #347
Summary
All public apis that call a random function accept a
random_seedkeyword argument. No seed is provided by default. The internal functions called by the public apis accept a seeded RNG object to ensure their results are the same given the same inputs and seed to the public API.random.Random()is used where possible as therngobject.np.random.default_rng()used elsewhere asnumpy_rngobject when an equivalentrandom.Random()function does not exist.Sets that are randomly sampled/sorted are ordered canonically to ensure a different PYTHONHASHSEED will not change results even if the same seed is given.
Why
VoteKit uses random functions or random number generators (RNGs) throughout to generate ballots/profiles, transfer votes, break ties, or sample preferences for candidates. When users simulate elections, they are unable to reproduce results as these random functions are unseeded. A seed argument was added to all public APIs that call random functions so that users can have reproducible results across election simulations.
Changes
cumulative.py: usesnumpy_rngfor multinomialname_bradley_terry.py: usesrng. choice -> choices for_inner_name_bradley_terry(), samples with replacement._inner_name_bradley_terry_mcmc()continues to use choicesname_plackett_luce.py: usesnumpy_rngfor choice: weighted sampling without replacement. No Python random stdlib equivalent function.pref_interval.py: usesnumpy_rngforfrom_dirichlet()dirichletutils.py:tiebreak_set()usesrngfor sample andtiebreak_ranking()passes alongrngtotiebreak_set()slate_utils.py: usesnumpy_rngforrandom(size)of_append_zero_slate_symbols()anduniform(low, high, size)of_fast_sample_without_replacement()ballot_generator.py:from_params()takes arandom_seedand initializes anumpy_rngwith seed and passes along toPreferenceInterval.from_dirichlet()slate_bradley_terry.py:_sample_bt_slate_ballots_deterministic()usesrngand choice->choices, sample with replacement,_sample_bt_slate_ballots_mcmc()usesrngand permutation->sample, choice->choices.inner_slate_bradley_terry()creates 2 random objects one withrandom.Randomand other withnp.random.default_rngwith the same seed if provided. Both use different algorithms for initialization.slate_plackett_luce.py: usesnumpy_rngdue touniform(size)core.py: usesnumpy_rngforPreferenceInterval.from_dirichlet()impartial_anon_culture.py: usesrngfor sampleimpartial_culture.py: usesrngfor_generate_profile_optimized_non_short()np.random.choice->sample: sample without replacement and randint for_generate_profile_optimized_with_short()spacial.pyusesnumpy_rngfor np.normal.uniform, normal, etc.transfers.py:random_transfer()usesrngfor sampleboosted_random_dictator.pyandrandom_dictator.py: usesrngfor choicesplurality_veto.pyusesrngand np.random.permutation->sampleplurality.py,plurality_veto.py,boosted_random_dictator.py,random_dictator.py,numpy_stv_base.py,stv.py,rating.pyaddself._rngfor random tiebreaksstv/utils.py: usesrng. np.random.choice-> sample for sampling without replacement. Store sample result as np.array to use numpy sort function.stv.py:STVclass takes atransferfunction. If set torandom_transfer()will need to use the_rngattribute fromSTVto have reproducible results if seeded. The rng will be binded torandom_transfer()viapartialso the positional argument check stays the same across fractional_transfer and random_transfer.Testing
test_rng_seed.pytests that all ballot generators and elections with random transfer or tiebreak are reproducible when a seed is given and non-deterministic if no seed is given. Tests were added where the PYTHONHASHSEED is changed per process for functions that act upon sets to ensure PYTHONHASHSEED does not change results when given the same seed for the RNG.