Debian Codegraph @

src/engine/rand.cpp

raw symbols SCIP
1 /***************************************************************************
2 * fheroes2: https://github.com/ihhub/fheroes2 *
3 * Copyright (C) 2019 - 2025 *
4 * *
5 * Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 *
6 * Copyright (C) 2009 by Andrey Afletdinov <[email protected]> *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23
24 #include "rand.h"
25
26 #include <numeric>
27 #include <random>
28
29 namespace
30 {
31 // Implementation of Fast Random Integer Generation in an Interval (https://arxiv.org/abs/1805.10941)
32 // NOTE: we can't use std::uniform_int_distribution here because it behaves differently on different platforms
33 uint32_t uniformIntInInterval( const uint32_t range, Rand::PCG32 & gen )
34 {
35 assert( range > 0 );
36
37 // Our implementation assumes our RNG can use the entire range of uint32_t
38 static_assert( Rand::PCG32::min() == 0 && Rand::PCG32::max() == std::numeric_limits<uint32_t>::max() );
39
40 uint32_t generated = gen();
41 uint64_t mult = ( static_cast<uint64_t>( generated ) * range );
42 uint32_t lowerPart = static_cast<uint32_t>( mult );
43
44 if ( lowerPart >= range ) {
45 const uint32_t upperPart = static_cast<uint32_t>( mult >> 32 );
46 assert( upperPart <= range );
47
48 return upperPart;
49 }
50
51 // This is the same as (2**32 - range) % range in the two's complement representation
52 const uint32_t discardBound = static_cast<uint32_t>( ~( range - 1 ) ) % range;
53
54 while ( lowerPart < discardBound ) {
55 generated = gen();
56 mult = ( static_cast<uint64_t>( generated ) * range );
57 lowerPart = static_cast<uint32_t>( mult );
58 }
59
60 const uint32_t upperPart = static_cast<uint32_t>( mult >> 32 );
61 assert( upperPart <= range );
62
63 return upperPart;
64 }
65 }
66
67 uint32_t Rand::uniformIntDistribution( const uint32_t from, const uint32_t to, PCG32 & gen )
68 {
69 if ( from == to ) {
70 return from;
71 }
72
73 assert( from < to );
74 const uint32_t rangeExclusive = to - from;
75 // If the range is the entire uint32_t (from 0 to 2**32-1), we can just return a random number
76 if ( rangeExclusive == std::numeric_limits<uint32_t>::max() ) {
77 static_assert( PCG32::min() == 0 && PCG32::max() == std::numeric_limits<uint32_t>::max() );
78
79 return gen();
80 }
81
82 return from + uniformIntInInterval( rangeExclusive + 1, gen );
83 }
84
85 Rand::PCG32 & Rand::CurrentThreadRandomDevice()
86 {
87 thread_local std::random_device rd;
88 thread_local PCG32 gen( rd );
89
90 return gen;
91 }
92
93 uint32_t Rand::Get( uint32_t from, uint32_t to /* = 0 */ )
94 {
95 if ( from > to ) {
96 std::swap( from, to );
97 }
98
99 return uniformIntDistribution( from, to, CurrentThreadRandomDevice() );
100 }
101
102 uint32_t Rand::GetWithSeed( uint32_t from, uint32_t to, uint32_t seed )
103 {
104 if ( from > to ) {
105 std::swap( from, to );
106 }
107
108 PCG32 seededGen( seed );
109 return uniformIntDistribution( from, to, seededGen );
110 }
111
112 uint32_t Rand::GetWithGen( uint32_t from, uint32_t to, PCG32 & gen )
113 {
114 if ( from > to ) {
115 std::swap( from, to );
116 }
117
118 return uniformIntDistribution( from, to, gen );
119 }
120
121 int32_t Rand::Queue::Get( const std::function<uint32_t( uint32_t )> & randomFunc ) const
122 {
123 if ( empty() ) {
124 return 0;
125 }
126
127 const uint32_t sum = std::accumulate( begin(), end(), static_cast<uint32_t>( 0 ), []( const uint32_t total, const ValueWeight & vw ) { return total + vw.second; } );
128 assert( sum > 0 );
129
130 uint32_t rand = randomFunc( sum - 1 );
131 assert( rand < sum );
132
133 for ( const auto & [value, weight] : *this ) {
134 if ( rand < weight ) {
135 return value;
136 }
137
138 rand -= weight;
139 }
140
141 assert( 0 );
142
143 return 0;
144 }