Skip to content

Commit 9392ac5

Browse files
author
Dave Abrahams
authored
Merge pull request #6239 from hughbe/stubs-rand
Port stdlib/public/stubs/GlobalObjects to Windows
2 parents 2302f59 + aeaf046 commit 9392ac5

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

stdlib/public/stubs/GlobalObjects.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/Runtime/Metadata.h"
2121
#include "swift/Runtime/Debug.h"
2222
#include <stdlib.h>
23+
#include <random>
2324

2425
namespace swift {
2526
// FIXME(ABI)#76 : does this declaration need SWIFT_RUNTIME_STDLIB_INTERFACE?
@@ -106,23 +107,10 @@ swift::_SwiftEmptySetStorage swift::_swiftEmptySetStorage = {
106107
};
107108

108109
static __swift_uint64_t randomUInt64() {
109-
#if defined(__APPLE__)
110-
return static_cast<__swift_uint64_t>(arc4random()) |
111-
(static_cast<__swift_uint64_t>(arc4random()) << 32);
112-
#else
113-
auto devUrandom = fopen("/dev/urandom", "r");
114-
if (!devUrandom) {
115-
swift::fatalError(/* flags = */ 0, "Opening \"/dev/urandom\" failed");
116-
}
117-
uint64_t result;
118-
if (fread(&result, sizeof(result), 1, devUrandom) != 1) {
119-
swift::fatalError(/* flags = */ 0, "Reading from \"/dev/urandom\" failed");
120-
}
121-
if (fclose(devUrandom)) {
122-
swift::fatalError(/* flags = */ 0, "Closing \"/dev/urandom\" failed");
123-
}
124-
return result;
125-
#endif
110+
std::random_device randomDevice;
111+
std::mt19937_64 twisterEngine(randomDevice());
112+
std::uniform_int_distribution<__swift_uint64_t> distribution;
113+
return distribution(twisterEngine);
126114
}
127115

128116
SWIFT_ALLOWED_RUNTIME_GLOBAL_CTOR_BEGIN

0 commit comments

Comments
 (0)