2023-01-04 14:35:57 -05:00
|
|
|
/*
|
|
|
|
* Created by Brett on 04/01/23.
|
|
|
|
* Licensed under GNU General Public License V3.0
|
|
|
|
* See LICENSE file for license detail
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BLT_RANDOM_H
|
|
|
|
#define BLT_RANDOM_H
|
|
|
|
|
2024-07-09 14:11:07 -04:00
|
|
|
#include <blt/std/types.h>
|
2023-01-04 14:35:57 -05:00
|
|
|
#include <random>
|
|
|
|
|
2024-07-09 14:11:07 -04:00
|
|
|
namespace blt::random
|
|
|
|
{
|
|
|
|
// https://github.com/avaneev/komihash/tree/main
|
2023-07-23 13:10:11 -04:00
|
|
|
|
2024-07-09 14:11:07 -04:00
|
|
|
static inline blt::u32 PCG_Hash(blt::u32 input)
|
|
|
|
{
|
|
|
|
blt::u32 state = input * 747796405u + 2891336453u;
|
|
|
|
blt::u32 word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
|
|
|
|
return (word >> 22u) ^ word;
|
2023-07-23 13:10:11 -04:00
|
|
|
}
|
2024-07-09 14:11:07 -04:00
|
|
|
|
|
|
|
//https://lemire.me/blog/2018/08/15/fast-strongly-universal-64-bit-hashing-everywhere/
|
|
|
|
static inline blt::u64 murmur64(blt::u64 h)
|
|
|
|
{
|
|
|
|
h ^= h >> 33;
|
|
|
|
h *= 0xff51afd7ed558ccdL;
|
|
|
|
h ^= h >> 33;
|
|
|
|
h *= 0xc4ceb9fe1a85ec53L;
|
|
|
|
h ^= h >> 33;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double pcg_double(blt::u32& seed)
|
|
|
|
{
|
2023-07-23 13:10:11 -04:00
|
|
|
seed = PCG_Hash(seed);
|
2024-07-09 14:11:07 -04:00
|
|
|
return static_cast<double>(seed) / static_cast<double>(std::numeric_limits<blt::u32>::max());
|
2023-07-23 13:10:11 -04:00
|
|
|
}
|
2024-07-09 14:11:07 -04:00
|
|
|
|
|
|
|
static inline float pcg_float(blt::u32& seed)
|
|
|
|
{
|
|
|
|
return static_cast<float>(pcg_double(seed));
|
|
|
|
}
|
|
|
|
|
2023-07-23 13:10:11 -04:00
|
|
|
/**
|
|
|
|
* @return random float without changing seed
|
|
|
|
*/
|
2024-07-09 14:11:07 -04:00
|
|
|
static inline float pcg_float_c(blt::u32 seed)
|
|
|
|
{
|
|
|
|
return pcg_float(seed);
|
2023-07-23 13:10:11 -04:00
|
|
|
}
|
2024-07-09 14:11:07 -04:00
|
|
|
|
|
|
|
static inline double pcg_double_c(blt::u32 seed)
|
|
|
|
{
|
|
|
|
return pcg_double(seed);
|
|
|
|
}
|
|
|
|
|
2023-07-23 13:10:11 -04:00
|
|
|
/**
|
|
|
|
* @param seed seed for random
|
|
|
|
* @param min inclusive min
|
|
|
|
* @param max exclusive max
|
|
|
|
* @return random int between min (inclusive) and max (exclusive)
|
|
|
|
*/
|
2024-07-09 14:11:07 -04:00
|
|
|
static inline int pcg_int(blt::u32& seed, int min = 0, int max = 2)
|
|
|
|
{
|
|
|
|
return static_cast<int>((pcg_double(seed) * static_cast<double>(max - min)) + static_cast<double>(min));
|
2023-07-23 13:10:11 -04:00
|
|
|
}
|
2024-07-09 14:11:07 -04:00
|
|
|
|
|
|
|
static inline int pcg_int_c(blt::u32 seed, int min = 0, int max = 2)
|
|
|
|
{
|
|
|
|
return pcg_int(seed, min, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline double murmur_double(blt::u64& seed)
|
|
|
|
{
|
|
|
|
seed = murmur64(seed);
|
|
|
|
return static_cast<double>(seed) / static_cast<double>(std::numeric_limits<blt::u64>::max());
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float murmur_float(blt::u64& seed)
|
|
|
|
{
|
|
|
|
return static_cast<float>(murmur_double(seed));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float murmur_float_c(blt::u64 seed)
|
|
|
|
{
|
|
|
|
return murmur_float(seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double murmur_double_c(blt::u64 seed)
|
|
|
|
{
|
|
|
|
return murmur_double(seed);
|
2023-07-23 13:10:11 -04:00
|
|
|
}
|
2023-03-05 12:00:45 -05:00
|
|
|
|
|
|
|
template<typename T>
|
2024-07-09 14:11:07 -04:00
|
|
|
static inline T murmur_integral_64(blt::u64& seed, T min = 0, T max = 2)
|
|
|
|
{
|
|
|
|
return static_cast<T>((murmur_double(seed) * static_cast<double>(max - min)) + static_cast<double>(min));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static inline T murmur_integral_64c(blt::u64 seed, T min = 0, T max = 2)
|
|
|
|
{
|
|
|
|
return murmur_integral_64(seed, min, max);
|
|
|
|
}
|
2023-07-23 13:10:11 -04:00
|
|
|
|
2023-01-04 14:35:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BLT_RANDOM_H
|