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.
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
Add rand, srand, and rand_r implementations #31
Add rand, srand, and rand_r implementations #31
Changes from 5 commits
a1f810e
bd5c82f
329b743
f109f21
9ff85c7
bc612e3
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this required? No other accesses are gated on this CAS operation so I think Relaxed is OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, I am not sure and wanted to play it safe. I basically copied the code from the docs. If you are sure, I can change it though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading https://marabos.nl/atomics/memory-ordering.html I'm pretty sure that Relaxed for both is OK here. We're only modifying that single value, and nothing else related to it. So there are no other writes that must happen strictly before or strictly after we modify this one value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be odd.
As you're pulling 31 random bits, I'm pretty sure this should be 0x7fff_ffff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested all
2^32
possible seeds and the value never went over0x7FFF_FFFC
. This seemed strange to me and I didn't really know what to do withRAND_MAX
in that case. The original algorithm goes to up0x7FFF_FFFD
, but does not include0
. That is why I subtracted1
, because I thought it would be way more unexpected to never get0
then to never get0x7FFF_FFFD
.Since it may be odd to have
RAND_MAX
not be2^n - 1
, we could either reduce it to 30 bits at0x3FFF_FFFF
or "lie" and set it to0x7FFF_FFFF
which could be bad, because knowingRAND_MAX
is necessary if you want to userand
to get a non-biased distribution.Since
tinyrlibc
is mainly used by C code,RAND_MAX
will probably never be used directly, because in CRAND_MAX
is a ´#defineanyway, so this value will not be seen by the code using
rand`.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How many times did you call rand()? The 10 or 11 bits that get pulled should be relatively uniform. It might be worth logging them individually. Then if they are uniform, you should get values from 0 to 2^31 - 1 by combining 31 random bits together.