Skip to content

randomize: added doAssert(seed!=0) to avoid invalid (non-random) behavior #10635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2019

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented Feb 11, 2019

/cc @demotomohiro

State of xoroshiro128+ must not be 0 or it generates 0 forever.

I ran into this gotcha before: after calling randomize(0), rand(int) generates 0 forever; this PR enforces that seed is nonzero to avoid puzzling behavior.

#10546 (comment) added a documentation for the fact that seed needs to be nonzero but this PR enforces this with a doAssert.
I'm not sure there's any valid use case for randomize(0) (which generates constant, non-random data) but if there is one, IMO it'd be better via a different API, eg initNonRandom() or another name, otherwise it's error prone (easy to miss this point in docs)

@Araq Araq merged commit bdc150a into nim-lang:devel Feb 12, 2019
@timotheecour timotheecour deleted the pr_randomize_assert_nonzero branch February 12, 2019 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants