mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
nearby: snapshot as of cl/296436629
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: I2cf5bf225b76f4c1541954651f3a7544a14e0cec
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
#include "platform/prng.h"
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include "absl/time/clock.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
#define UNSIGNED_INT_BITMASK (std::numeric_limits<unsigned int>::max())
|
||||
|
||||
Prng::Prng() {
|
||||
// absl::GetCurrentTimeNanos() returns 64 bits, but srand() wants an unsigned
|
||||
// int, so we may have to lose some of those 64 bits.
|
||||
//
|
||||
// The lower bits of the current-time-in-nanos are likely to have more entropy
|
||||
// than the upper bits, so choose the former.
|
||||
srand(static_cast<unsigned int>(absl::GetCurrentTimeNanos() &
|
||||
UNSIGNED_INT_BITMASK));
|
||||
}
|
||||
|
||||
Prng::~Prng() {
|
||||
// Nothing to do.
|
||||
}
|
||||
|
||||
#define RANDOM_BYTE (rand() & 0x0FF) // NOLINT
|
||||
|
||||
std::int32_t Prng::nextInt32() {
|
||||
return (static_cast<std::int32_t>(RANDOM_BYTE) << 24) |
|
||||
(static_cast<std::int32_t>(RANDOM_BYTE) << 16) |
|
||||
(static_cast<std::int32_t>(RANDOM_BYTE) << 8) |
|
||||
(static_cast<std::int32_t>(RANDOM_BYTE));
|
||||
}
|
||||
|
||||
std::uint32_t Prng::nextUInt32() {
|
||||
return static_cast<std::uint32_t>(nextInt32());
|
||||
}
|
||||
|
||||
std::int64_t Prng::nextInt64() {
|
||||
return (static_cast<std::int64_t>(nextInt32()) << 32) |
|
||||
(static_cast<std::int64_t>(nextInt32()));
|
||||
}
|
||||
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
Reference in New Issue
Block a user