MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / RandomSource

Class RandomSource

source/core/StarRandom.hpp:14–84  ·  view source on GitHub ↗

Deterministic random number source. Uses multiply-with-carry algorithm. Much higher quality than the predictable random number generators. Not thread safe (won't crash or anything, but might return less than optimal values).

Source from the content-addressed store, hash-verified

12// thread safe (won't crash or anything, but might return less than optimal
13// values).
14class RandomSource {
15public:
16 // Generates a RandomSource with a seed from Random::randu64()
17 RandomSource();
18 RandomSource(uint64_t seed);
19
20 // Re-initializes the random number generator using the given seed. It is
21 // exactly equivalent to constructing a new RandomSource, just using the same
22 // buffer.
23 void init();
24 void init(uint64_t seed);
25
26 void addEntropy();
27 void addEntropy(uint64_t seed);
28
29 uint32_t randu32();
30 uint64_t randu64();
31
32 int32_t randi32();
33 int64_t randi64();
34
35 // Generates values in the range [0.0, 1.0]
36 float randf();
37 // Generates values in the range [0.0, 1.0]
38 double randd();
39
40 // Random integer from [0, max], max must be >= 0
41 int64_t randInt(int64_t max);
42 uint64_t randUInt(uint64_t max);
43
44 // Random integer from [min, max]
45 int64_t randInt(int64_t min, int64_t max);
46 uint64_t randUInt(uint64_t min, uint64_t max);
47
48 float randf(float min, float max);
49 double randd(double min, double max);
50
51 bool randb();
52
53 // Generates values via normal distribution with box-muller algorithm
54 float nrandf(float stddev = 1.0f, float mean = 0.0f);
55 double nrandd(double stddev = 1.0, double mean = 0.0);
56
57 // Round a fractional value statistically towards the floor or ceiling. For
58 // example, if a value is 5.2, 80% of the time it will round to 5, but 20% of
59 // the time it will round to 6.
60 int64_t stochasticRound(double val);
61
62 void randBytes(char* buf, size_t len);
63 ByteArray randBytes(size_t len);
64
65 // Pick a random value out of a container
66 template <typename Container>
67 typename Container::value_type const& randFrom(Container const& container);
68 template <typename Container>
69 typename Container::value_type& randFrom(Container& container);
70 template <typename Container>
71 typename Container::value_type randValueFrom(Container const& container);

Callers 10

generateNameMethod · 0.85
produceMonsterMethod · 0.85
initMethod · 0.85
handleIncomingPacketMethod · 0.85
spawnObjectsMethod · 0.85
sendEventMethod · 0.85
updateMethod · 0.85
mainFunction · 0.85
doInitFunction · 0.85
makeUtilityCallbacksMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected