File: Random.cpp

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (137 lines) | stat: -rw-r--r-- 3,902 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "Random.h"
#include "../../Main.h"

#include <chrono>
#include <cmath>	// for ldexp()
#include <cstring>
#include <tuple>

#if defined(DEATH_TARGET_WINDOWS)
#	include <objbase.h>
#endif

namespace nCine
{
	namespace
	{
		const std::uint64_t DefaultInitState = 0x853c49e6748fea9bULL;
		const std::uint64_t DefaultInitSequence = 0xda3e39cb94b95bdbULL;

		std::uint32_t random(std::uint64_t& state, std::uint64_t& increment) noexcept
		{
			const std::uint64_t oldState = state;
			state = oldState * 6364136223846793005ULL + increment;
			const std::uint32_t xorShifted = static_cast<std::uint32_t>(((oldState >> 18u) ^ oldState) >> 27u);
			const std::uint32_t rotation = static_cast<std::uint32_t>(oldState >> 59u);
			return (xorShifted >> rotation) | (xorShifted << ((std::uint32_t)(-(std::int32_t)rotation) & 31));
		}

		std::uint32_t boundRandom(std::uint64_t& state, std::uint64_t& increment, std::uint32_t bound) noexcept
		{
			const std::uint32_t threshold = (std::uint32_t)(-(std::int32_t)bound) % bound;
			while (true) {
				const std::uint32_t r = random(state, increment);
				if (r >= threshold) {
					return r % bound;
				}
			}
		}
	}

	RandomGenerator& Random() noexcept
	{
		static RandomGenerator instance;
		return instance;
	}

	RandomGenerator::RandomGenerator() noexcept
		: _state(0ULL), _increment(0ULL)
	{
		std::uint64_t now = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now()).time_since_epoch().count();
		Init(DefaultInitState ^ now, DefaultInitSequence);
	}

	RandomGenerator::RandomGenerator(std::uint64_t initState, std::uint64_t initSequence) noexcept
		: _state(0ULL), _increment(0ULL)
	{
		Init(initState, initSequence);
	}

	void RandomGenerator::Init(std::uint64_t initState, std::uint64_t initSequence) noexcept
	{
		_state = 0U;
		_increment = (initSequence << 1u) | 1u;
		random(_state, _increment);
		_state += initState;
		random(_state, _increment);
	}

	std::uint32_t RandomGenerator::Next() noexcept
	{
		return random(_state, _increment);
	}

	std::uint32_t RandomGenerator::Next(std::uint32_t min, std::uint32_t max) noexcept
	{
		if (min == max) {
			return min;
		} else {
			return min + boundRandom(_state, _increment, max - min);
		}
	}

	float RandomGenerator::NextFloat() noexcept
	{
		return static_cast<float>(ldexp(random(_state, _increment), -32));
	}

	float RandomGenerator::NextFloat(float min, float max) noexcept
	{
		return min + static_cast<float>(ldexp(random(_state, _increment), -32)) * (max - min);
	}

	bool RandomGenerator::NextBool() noexcept
	{
		return (boundRandom(_state, _increment, 2) != 0);
	}

	std::uint32_t RandomGenerator::Fast(std::uint32_t min, std::uint32_t max) noexcept
	{
		return (min == max ? min : min + random(_state, _increment) % (max - min));
	}

	float RandomGenerator::FastFloat() noexcept
	{
		return static_cast<float>(random(_state, _increment) / static_cast<float>(UINT32_MAX));
	}

	float RandomGenerator::FastFloat(float min, float max) noexcept
	{
		return min + (static_cast<float>(random(_state, _increment)) / static_cast<float>(UINT32_MAX)) * (max - min);
	}

	void RandomGenerator::Uuid(Containers::StaticArrayView<16, std::uint8_t> result)
	{
#if defined(DEATH_TARGET_WINDOWS)
		GUID guid;
		std::ignore = ::CoCreateGuid(&guid);
		static_assert(sizeof(guid) == 16);
		std::memcpy(result.data(), &guid, sizeof(GUID));
#else
		std::uint64_t now = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now()).time_since_epoch().count();
		std::uint64_t timeMs = now / 1000;
		std::uint64_t timeUs = now % 1000;
		std::uint64_t timestamp = (timeMs << 16) | 0x7000 | timeUs;

		std::uint32_t i1 = Next();
		std::uint32_t i2 = Next();

		std::memcpy(&result[0], &timestamp, 8);
		std::memcpy(&result[8], &i1, 4);
		std::memcpy(&result[12], &i2, 4);

		// Variant
		result[8] = (result[8] & 0x3F) | 0x80;
#endif
	}
}