Created
July 21, 2022 01:01
-
-
Save hugsy/cc6d34b050fdee65e0a6373c18ba6e69 to your computer and use it in GitHub Desktop.
Basic (bad) experiments for a constexpr rand() function
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
// | |
// Requires C++20 | |
// | |
#include <cstdint> | |
#include <iostream> | |
#include <string> | |
constexpr static auto to_int(const char* str, int offset) { | |
return static_cast<std::uint32_t>(str[offset] - '0') * 10 + | |
static_cast<std::uint32_t>(str[offset + 1] - '0'); | |
} | |
constexpr static uint64_t seed() { | |
auto t = __TIME__; | |
uint64_t l = to_int(t, 0) * 60 * 60 ; | |
l+= to_int(t, 3) * 60; | |
l+= to_int(t, 6); | |
return l; | |
} | |
consteval static uint64_t xorshift64(uint64_t seed) | |
{ | |
uint64_t x = seed; | |
x ^= x << 13; | |
x ^= x >> 17; | |
x ^= x << 5; | |
return x; | |
} | |
constinit const static uint64_t g_seed = seed(); | |
consteval static uint64_t rand(uint64_t level) | |
{ | |
if(level == 0) | |
return xorshift64(g_seed); | |
return xorshift64(rand(level-1)); | |
} | |
int main() | |
{ | |
std::cout << rand(1024) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment