/
SneakBug8
/
Project-Alice
Обзор
Документация
Войти
/
SneakBug8
/
Project-Alice
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/common_types/prng.cpp
80 строк
3 KB
Katy
Update prng.cpp
24 ноя 2024, 09:04
Не верифицирован
24 ноя 2024, 09:04
ce520a3
Код
Авторство
О чём код?
#include "prng.hpp" #include "system_state.hpp" #include "random123/philox.h" namespace rng { uint64_t get_random(sys::state const& state, uint32_t value_in) { // try to populate as many bits of value_in as you can r123::Philox4x32 rng; r123::Philox4x32::ctr_type c = {state.current_date.value, value_in, 0, 0 }; r123::Philox4x32::key_type k = {state.game_seed, 0x3918CA23}; r123::Philox4x32::ctr_type r = rng(c, k); return (uint64_t(r[0]) << 32) | uint64_t(r[1]); } uint64_t get_random(sys::state const& state, uint32_t value_in_hi, uint32_t value_in_lo) { r123::Philox4x32 rng; r123::Philox4x32::ctr_type c = {value_in_hi, value_in_lo, 0, 0}; r123::Philox4x32::key_type k = {state.game_seed, 0x3918CA23 }; r123::Philox4x32::ctr_type r = rng(c, k); return (uint64_t(r[0]) << 32) | uint64_t(r[1]); } random_pair get_random_pair(sys::state const& state, uint32_t value_in) { // each call natively generates 128 random bits anyways r123::Philox4x32 rng; r123::Philox4x32::ctr_type c = {state.current_date.value, value_in, 0, 0 }; r123::Philox4x32::key_type k = {state.game_seed, 0x3918CA23 }; r123::Philox4x32::ctr_type r = rng(c, k); return random_pair{(uint64_t(r[0]) << 32) | uint64_t(r[1]), (uint64_t(r[2]) << 32) | uint64_t(r[3])}; } random_pair get_random_pair(sys::state const& state, uint32_t value_in_hi, uint32_t value_in_lo) { // each call natively generates 128 random bits anyways r123::Philox4x32 rng; r123::Philox4x32::ctr_type c = {value_in_hi, value_in_lo, 0, 0 }; r123::Philox4x32::key_type k = {state.game_seed, 0x3918CA23 }; r123::Philox4x32::ctr_type r = rng(c, k); return random_pair{(uint64_t(r[0]) << 32) | uint64_t(r[1]), (uint64_t(r[2]) << 32) | uint64_t(r[3])}; } uint32_t reduce(uint32_t value_in, uint32_t upper_bound) { return uint32_t((uint64_t(value_in) * uint64_t(upper_bound)) >> 32); } float get_random_float(sys::state const& state, uint32_t value_in) { r123::Philox4x32 rng; r123::Philox4x32::ctr_type c = {state.current_date.value, value_in, 0, 0 }; r123::Philox4x32::key_type k = {state.game_seed, 0x3918CA23}; r123::Philox4x32::ctr_type r = rng(c, k); // union { uint32_t u; float f; } pattern; pattern.u = 0x3f800000; pattern.u |= 0x7fffff & r[1]; return pattern.f - 1.0f; } float get_random_float(sys::state const& state, uint32_t value_in_hi, uint32_t value_in_lo) { r123::Philox4x32 rng; r123::Philox4x32::ctr_type c = {value_in_hi, value_in_lo, 0, 0 }; r123::Philox4x32::key_type k = {state.game_seed, 0x3918CA23}; r123::Philox4x32::ctr_type r = rng(c, k); // union { uint32_t u; float f; } pattern; pattern.u = 0x3f800000; pattern.u |= 0x7fffff & r[1]; return pattern.f - 1.0f; } } // namespace rng