-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathRandom.cpp
More file actions
24 lines (20 loc) · 812 Bytes
/
Random.cpp
File metadata and controls
24 lines (20 loc) · 812 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/core/utils/local/Random.h>
#include <cstddef>
#include <mutex>
#include <random>
#include <thread>
std::mt19937::result_type Aws::Utils::GetRandomValue() {
static size_t const processRandomSeed = std::random_device{}();
static std::mt19937 threadRandomSeedGen(processRandomSeed);
// Threads can be re-used (esp. on OS X), generate a true random per-thread random seed
static std::mutex threadRandomSeedGenMtx;
thread_local std::mt19937 gen([]() -> size_t {
std::unique_lock<std::mutex> const lock(threadRandomSeedGenMtx);
return static_cast<size_t>(std::hash<std::thread::id>{}(std::this_thread::get_id()) ^ threadRandomSeedGen());
}());
return gen();
}