constexprRandint() (コンパイル時乱数)
(Util/constexpr-randint.hpp)
Depends on
Code
#pragma once
#include <cstdint>
#include "./cstring-hash.hpp"
/**
* @brief constexprRandint() (コンパイル時乱数)
* 呼び出す度に値が変わるわけではないので う笑
*/
#define STRINGIZE_I(x) #x
#define STRINGIZE(x) STRINGIZE_I(x)
#define UNIQUE_STRING __DATE__ "_" __TIME__ "_" __FILE__ "_" STRINGIZE(__LINE__)
constexpr uint32_t constexprRandint(uint32_t min, uint32_t max) {
const auto m = max - min + 1;
return cstringHash(UNIQUE_STRING) % m + min;
}
#line 2 "Util/constexpr-randint.hpp"
#include <cstdint>
#line 2 "Util/cstring-hash.hpp"
#line 4 "Util/cstring-hash.hpp"
/**
* @brief cstringHash() ヌル文字終端された文字列から32bitハッシュ値を生成する(constexpr)
* @see https://en.wikipedia.org/wiki/Jenkins_hash_function
*/
constexpr uint32_t cstringHash(const char* s) {
uint32_t hashv = 0;
while (*s != '\0') {
hashv += *s++;
hashv += hashv << 10;
hashv ^= hashv >> 6;
}
hashv += hashv << 3;
hashv ^= hashv >> 11;
hashv += hashv << 15;
return hashv;
}
#line 6 "Util/constexpr-randint.hpp"
/**
* @brief constexprRandint() (コンパイル時乱数)
* 呼び出す度に値が変わるわけではないので う笑
*/
#define STRINGIZE_I(x) #x
#define STRINGIZE(x) STRINGIZE_I(x)
#define UNIQUE_STRING __DATE__ "_" __TIME__ "_" __FILE__ "_" STRINGIZE(__LINE__)
constexpr uint32_t constexprRandint(uint32_t min, uint32_t max) {
const auto m = max - min + 1;
return cstringHash(UNIQUE_STRING) % m + min;
}
Back to top page