Conversation
…ity fixes Co-authored-by: feinorgh <3132980+feinorgh@users.noreply.github.com>
… random.c Co-authored-by: feinorgh <3132980+feinorgh@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Analyze code for performance and compatibility improvements
Fix statistical seed bias, off-by-one range errors, and resource handling bugs
Mar 5, 2026
feinorgh
approved these changes
Mar 5, 2026
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.
Several correctness and robustness bugs affected the quality of generated numbers and the valid parameter space. The most critical issue caused the RNG to be seeded with only 1 byte despite reading 256 bytes of entropy.
Statistical correctness
mpz_import(seed, 1, 1, 1, ...)imported only the first byte of the 256-byte entropy buffer. Fixed tompz_import(seed, count, 1, sizeof(char), ...).rangewas exclusive: Globalrangewas computed ashigh − lowthroughout, but the inclusive counthigh − low + 1is required for correct uniform sampling and inverse-method sizing. Now set once (correctly) inparse_options().Off-by-one bugs
while (mpz_cmp(tmp, high))never printedhigh. Fixed to<= 0.mpz_cmp(arg_count, range) >= 0rejected valid counts up to the full range size. Fixed to> 0(after correctingrangeto be inclusive).invsizeundercounted by 1: Was computed from the off-by-onerange, causing one fewer exclusion to be generated than needed.Resource handling
malloc(count * sizeof(char*) + 1)allocated 8× the needed bytes on 64-bit. Fixed tomalloc(count + 1).read()not retried: A singleread()from/dev/randommay return fewer bytes than requested. Added a retry loop with error and EOF checks.open()andmalloc()error checks ininit_random().mallocin the inverse path ofgenerate_series(); handlesinvsize == 0(full-range generation) safely.mpz_clear(range)called beforempz_init(range)when exiting via-h/-v/--usage. Fixed by movingmpz_init(range)tomain().mpz_get_str(NULL, ...)were never freed.Minor
breakaftercase 'r'(implicit fallthrough tocase '?'was harmless but misleading).highparameter fromrandom_integer()since the globalrangenow holds the inclusive count.-Wunused-parameterfordepthintreeaction()with(void)depth.💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.