flatten 20260225

This commit is contained in:
Timothy Prepscius
2026-02-25 12:39:24 -05:00
commit fa54be052a
315 changed files with 49791 additions and 0 deletions

18
tjp/core/random/Random.cpp Executable file
View File

@@ -0,0 +1,18 @@
// TJP COPYRIGHT HEADER
#ifdef TJP_CORE_HEADER_ONLY
#pragma once
#endif
#include <tjp/core/header_only/compile.h>
#include "Random.h"
namespace tjp::core {
StandardRandomAdapter StandardRandom;
RealRandom StandardRealRandom;
} // namespace

236
tjp/core/random/Random.h Executable file
View File

@@ -0,0 +1,236 @@
// TJP COPYRIGHT HEADER
#pragma once
#include <tjp/core/types/Types.h>
#include <algorithm>
#include <tjp/core/algorithm/numeric_limits.hpp>
#include <tjp/core/system/System.h>
#define HAS_CPP_11
#ifdef HAS_CPP_11
#include <random>
#else
#include <stdlib.h>
namespace std {
class default_random_engine {
public:
char operator ()() {
return random();
}
void seed(int seed=0) {}
} ;
typedef default_random_engine random_device;
}
#endif
namespace tjp {
namespace core {
namespace random_detail {
template<typename T>
T abs_(T t) { return std::abs(t); }
template<>
inline
s128 abs_(s128 t) { return t > 0 ? t : -t; }
template<>
inline
constexpr u64 abs_(u64 t) { return t; }
template<>
inline
constexpr u32 abs_(u32 t) { return t; }
template<>
inline
constexpr u16 abs_(u16 t) { return t; }
template<>
inline
constexpr u8 abs_(u8 t) { return t; }
#if defined(SYS_APPLE) || defined(SYS_IOS)
template<>
inline
constexpr size_t abs_(size_t t) { return t; }
#endif
} //
template<typename G>
class Random {
public:
using Generator = G;
Generator generator;
template<typename T>
T next () {
T t;
char *begin = (char *)&t;
char *end = begin + sizeof(T);
fill(begin, end);
return t;
}
public:
Random() {};
Random(Generator &&generator_) :
generator(generator_)
{
}
auto generate()
{
return generator();
}
char nextChar ()
{
return next<char>();
}
template<typename T>
T nextIndex (T r)
{
return nextInt<T>() % r;
}
template<typename T=int>
T nextInt ()
{
return random_detail::abs_(next<T>());
}
template<typename T=int>
T nextInt (T l, T r)
{
auto d = r - l + 1;
return (random_detail::abs_(next<T>()) % d) + l;
}
template<typename T>
T nextReal (T min=0, T max=1)
{
auto d = max - min;
constexpr auto random_max = numeric_limits_ext<T>::random_max;
auto l = T(nextInt<u64>(0, random_max));
auto r = T(random_max);
auto v = l/r * d + min;
return v;
}
template<typename T=int>
T nextPositiveBits (size_t numBits)
{
auto v = next<T>();
auto zeroBits = sizeof(v) * 8 - numBits;
v = (v << zeroBits) >> zeroBits;
return random_detail::abs_(v);
}
template<typename T=int>
T nextBits (size_t numBits)
{
auto v = next<T>();
auto zeroBits = sizeof(v) * 8 - numBits;
v = (v << zeroBits) >> zeroBits;
return v;
}
void fill (char *begin, char *end)
{
if (begin == end)
return;
auto *i = begin;
while (true)
{
auto v = generate();
for (auto j=0; j<sizeof(v); ++j)
{
*i++ = (u8)v;
v = v >> 8;
if (i == end)
return;
}
}
}
};
template<typename T>
class RandomGeneratorSeeded
{
public:
using Generator = T;
Generator random;
public:
RandomGeneratorSeeded () :
random(std::random_device()())
{
}
RandomGeneratorSeeded(int seed) :
random(seed)
{
}
auto operator()()
{
return random();
}
} ;
using ReliableRandom = Random<RandomGeneratorSeeded<std::mt19937>>;
using RealRandom = ReliableRandom;
using FastRandom = Random<RandomGeneratorSeeded<std::default_random_engine>>;
using StandardRandomAdapter = FastRandom;
extern StandardRandomAdapter StandardRandom;
extern RealRandom StandardRealRandom;
#if 1
template <class _RandomAccessIterator>
void random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
{
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(__first, __last, g);
}
#else
template <class _RandomAccessIterator>
void random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
{
std::random_shuffle(__first, __last);
}
#endif
} // namespace
} // namespace
#ifdef TJP_CORE_HEADER_ONLY_SINGLE_FILE
#include "Random.cpp"
#endif

46
tjp/core/random/bell_curve.hpp Executable file
View File

@@ -0,0 +1,46 @@
// TJP COPYRIGHT HEADER
#pragma once
#include <cmath>
#include "Random.h"
#include <tjp/core/math/clamp.hpp>
namespace tjp::core::bell_curve {
template<typename T>
T sigmoid(T x) {
constexpr T one(1);
return one / (one + std::exp(-x));
}
template<typename T>
T random(T z, T k)
{
return sigmoid(k * z);
}
template<typename T>
T random(T z)
{
return random(z, T(3));
}
template<typename T>
T random()
{
std::normal_distribution<> dist(0.0, 1.0);
auto z = (T)dist(StandardRandom.generator.random);
return random(z);
}
size_t random_index(size_t s)
{
auto r = random<double>();
auto i = size_t(r * s);
return math::clamp(i, 0, s-1);
}
} // namespace

18
tjp/core/random/file_name.hpp Executable file
View File

@@ -0,0 +1,18 @@
// TJP COPYRIGHT HEADER
#pragma once
#include "Random.h"
#include <tjp/core/string/String.hpp>
#include <tjp/core/string/to_string.hpp>
namespace tjp::core::random {
inline
String file_name ()
{
return to_string(StandardRandom.next<u32>());
}
} // namespace

View File

@@ -0,0 +1,18 @@
// TJP COPYRIGHT HEADER
#pragma once
#include "Random.h"
#include <tjp/core/string/String.hpp>
#include <tjp/core/string/ToString.hpp>
namespace tjp::core::random {
inline
String temporary_path (const StringView &prefix)
{
return ToString() << "/tmp/" << prefix << '-' << to_string(StandardRandom.next<u32>()) << '/';
}
} // namespace