-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[libc] Improve the implementation of the rand() function #66131
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,11 +14,21 @@ | |
#include <stdlib.h> | ||
|
||
TEST(LlvmLibcRandTest, UnsetSeed) { | ||
static int vals[1000]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a larger array, so putting |
||
|
||
for (size_t i = 0; i < 1000; ++i) { | ||
int val = __llvm_libc::rand(); | ||
ASSERT_GE(val, 0); | ||
ASSERT_LE(val, RAND_MAX); | ||
vals[i] = val; | ||
} | ||
|
||
// The C standard specifies that if 'srand' is never called it should behave | ||
// as if 'srand' was called with a value of 1. If we seed the value with 1 we | ||
// should get the same sequence as the unseeded version. | ||
__llvm_libc::srand(1); | ||
jhuber6 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (size_t i = 0; i < 1000; ++i) | ||
ASSERT_EQ(__llvm_libc::rand(), vals[i]); | ||
} | ||
|
||
TEST(LlvmLibcRandTest, SetSeed) { | ||
|
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.
Shall we define it as INT_MAX instead?
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 wasn't sure if that would always be available when this header is included.
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.
given that the implementation shifts right by 32, I think it's likely safe to have this be written out.