-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add base/random module for random utilities.
- Loading branch information
1 parent
50d2d31
commit e8adbd1
Showing
7 changed files
with
84 additions
and
24 deletions.
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
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,42 @@ | ||
// This file is part of Desktop App Toolkit, | ||
// a set of libraries for developing nice desktop applications. | ||
// | ||
// For license and copyright information please follow this link: | ||
// https://github.com/desktop-app/legal/blob/master/LEGAL | ||
// | ||
#include "base/random.h" | ||
|
||
extern "C" { | ||
#include <openssl/rand.h> | ||
} // extern "C" | ||
|
||
namespace base { | ||
|
||
void RandomFill(bytes::span bytes) { | ||
const auto result = RAND_bytes( | ||
reinterpret_cast<unsigned char*>(bytes.data()), | ||
bytes.size()); | ||
|
||
Ensures(result); | ||
} | ||
|
||
int RandomIndex(int count) { | ||
Expects(count > 0); | ||
|
||
if (count == 1) { | ||
return 0; | ||
} | ||
const auto max = (std::numeric_limits<uint32>::max() / count) * count; | ||
while (true) { | ||
const auto random = RandomValue<uint32>(); | ||
if (random < max) { | ||
return int(random % count); | ||
} | ||
} | ||
} | ||
|
||
void RandomAddSeed(bytes::const_span bytes) { | ||
RAND_seed(bytes.data(), bytes.size()); | ||
} | ||
|
||
} // namespace base |
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,35 @@ | ||
// This file is part of Desktop App Toolkit, | ||
// a set of libraries for developing nice desktop applications. | ||
// | ||
// For license and copyright information please follow this link: | ||
// https://github.com/desktop-app/legal/blob/master/LEGAL | ||
// | ||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <type_traits> | ||
|
||
#include "base/bytes.h" | ||
|
||
namespace base { | ||
|
||
void RandomFill(bytes::span bytes); | ||
|
||
inline void RandomFill(void *data, std::size_t length) { | ||
RandomFill({ static_cast<bytes::type*>(data), length }); | ||
} | ||
|
||
template < | ||
typename T, | ||
typename = std::enable_if_t<std::is_trivially_copyable_v<T>>> | ||
[[nodiscard]] inline T RandomValue() { | ||
auto result = T(); | ||
RandomFill({ reinterpret_cast<std::byte*>(&result), sizeof(T) }); | ||
return result; | ||
} | ||
|
||
[[nodiscard]] int RandomIndex(int count); | ||
|
||
void RandomAddSeed(bytes::const_span bytes); | ||
|
||
} // namespace base |