This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
util: make rand a standalone util decoupled from rdsn runtime #163
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
de7417f
util: make rand a standalone util decoupled from rdsn runtime
25bee3e
fix header dependencies
04942e3
fix dsn_random32
645f30b
fix compilation errors
e205f80
fix comment
f1d6b83
add refactoring and fixes
43bd11d
minor fixes
2eaa16d
fix corner case
b691fcf
fix test
91f6303
Merge branch 'master' of https://github.com/XiaoMi/rdsn into dev2
214bcaf
code refactoring
7f16607
code refactoring
3add1ac
Merge branch 'master' into dev2
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved. | ||
// This source code is licensed under the Apache License Version 2.0, which | ||
// can be found in the LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <cstddef> | ||
#include <limits> | ||
|
||
namespace dsn { | ||
namespace rand { | ||
|
||
// uint64n returns, as an uint64_t, a non-negative pseudo-random number in [min, max]. | ||
extern uint64_t uint64in(uint64_t min, uint64_t max); | ||
|
||
// uint32n returns, as an uint32_t, a non-negative pseudo-random number in [min, max]. | ||
inline uint32_t uint32in(uint32_t min, uint32_t max) | ||
{ | ||
return static_cast<uint32_t>(uint64in(min, max)); | ||
} | ||
|
||
// uint64n returns, as an uint64_t, a non-negative pseudo-random number in [0, n). | ||
// If n == 0, it returns 0. | ||
inline uint64_t uint64n(uint64_t n) | ||
{ | ||
if (n == 0) | ||
return 0; | ||
return uint64in(0, n - 1); | ||
} | ||
|
||
// uint32n returns, as an uint32_t, a non-negative pseudo-random number in [0, n). | ||
// If n == 0, it returns 0. | ||
inline uint32_t uint32n(uint32_t n) { return static_cast<uint32_t>(uint64n(n)); } | ||
|
||
// uint32 returns a pseudo-random 32-bit value as a uint32_t. | ||
inline uint32_t uint32() { return uint32in(0, std::numeric_limits<uint32_t>::max()); } | ||
|
||
// uint64 returns a pseudo-random 64-bit value as a uint64_t. | ||
inline uint64_t uint64() { return uint64in(0, std::numeric_limits<uint64_t>::max()); } | ||
|
||
// float64 returns, as a double, a pseudo-random number in [0.0,1.0]. | ||
inline double float64n() { return uint64in(0, 1000000000) / 1000000000.0; } | ||
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. float64n的命名反而不如原来的probability好。因为概率一定是[0, 1]的,而float不一定是 |
||
|
||
extern void set_seed_thread_local_rng(uint64_t seed); | ||
|
||
} // namespace rand | ||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved. | ||
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. 文件名还是统一用cpp吧 |
||
// This source code is licensed under the Apache License Version 2.0, which | ||
// can be found in the LICENSE file in the root directory of this source tree. | ||
|
||
#include <dsn/utility/rand.h> | ||
#include <random> | ||
|
||
namespace dsn { | ||
namespace rand { | ||
|
||
thread_local std::ranlux48_base g_thread_local_rng(std::random_device{}()); | ||
|
||
/*extern*/ uint64_t uint64in(uint64_t min, uint64_t max) | ||
{ | ||
return std::uniform_int_distribution<uint64_t>(min, max)(g_thread_local_rng); | ||
} | ||
|
||
/*extern*/ void set_seed_thread_local_rng(uint64_t seed) { g_thread_local_rng.seed(seed); } | ||
|
||
} // namespace rand | ||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved. | ||
// This source code is licensed under the Apache License Version 2.0, which | ||
// can be found in the LICENSE file in the root directory of this source tree. | ||
|
||
#include <dsn/utility/rand.h> | ||
#include <gtest/gtest.h> | ||
#include <thread> | ||
|
||
namespace dsn { | ||
|
||
TEST(random, sanity) | ||
{ | ||
{ // edge cases | ||
ASSERT_EQ(rand::uint64n(0), 0); | ||
ASSERT_EQ(rand::uint64in(0, 0), 0); | ||
ASSERT_EQ(rand::uint32n(0), 0); | ||
ASSERT_EQ(rand::uint32in(0, 0), 0); | ||
|
||
ASSERT_EQ(rand::uint64in(12, 12), 12); | ||
ASSERT_EQ(rand::uint32in(12, 12), 12); | ||
} | ||
|
||
constexpr int kTestSize = 1000; | ||
|
||
{ // 32-bit repeatability, uniqueness | ||
rand::set_seed_thread_local_rng(0xdeadbeef); | ||
std::vector<uint32_t> vals(kTestSize); | ||
for (int i = 0; i < kTestSize; ++i) { | ||
vals[i] = rand::uint32(); | ||
} | ||
|
||
rand::set_seed_thread_local_rng(0xdeadbeef); | ||
for (int i = 0; i < kTestSize; ++i) { | ||
ASSERT_EQ(rand::uint32(), vals[i]); | ||
} | ||
} | ||
|
||
{ // 64-bit repeatability, uniqueness | ||
rand::set_seed_thread_local_rng(0xdeadbeef); | ||
std::vector<uint64_t> vals(kTestSize); | ||
for (int i = 0; i < kTestSize; ++i) { | ||
vals[i] = rand::uint64(); | ||
} | ||
|
||
rand::set_seed_thread_local_rng(0xdeadbeef); | ||
for (int i = 0; i < kTestSize; ++i) { | ||
ASSERT_EQ(rand::uint64(), vals[i]); | ||
} | ||
} | ||
} | ||
|
||
TEST(random, multi_threaded) | ||
{ | ||
const int n = 100; | ||
std::vector<uint32_t> seeds(n); | ||
std::vector<std::thread> threads; | ||
for (int i = 0; i < n; ++i) { | ||
threads.push_back(std::thread([i, &seeds] { seeds[i] = rand::uint32(); })); | ||
} | ||
for (auto &t : threads) { | ||
t.join(); | ||
} | ||
std::sort(seeds.begin(), seeds.end()); | ||
for (int i = 0; i < n - 1; ++i) { | ||
EXPECT_LT(seeds[i], seeds[i + 1]); | ||
} | ||
} | ||
|
||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
注视是uint64n, 代码是uint64in。
不过这命名没看懂,n和in代表啥?