Changeset View
Changeset View
Standalone View
Standalone View
source/lib/rand.h
/* Copyright (C) 2010 Wildfire Games. | /* Copyright (C) 2020 Wildfire Games. | ||||||||||||||||||||||||||||||||||
* | * | ||||||||||||||||||||||||||||||||||
* Permission is hereby granted, free of charge, to any person obtaining | * Permission is hereby granted, free of charge, to any person obtaining | ||||||||||||||||||||||||||||||||||
* a copy of this software and associated documentation files (the | * a copy of this software and associated documentation files (the | ||||||||||||||||||||||||||||||||||
* "Software"), to deal in the Software without restriction, including | * "Software"), to deal in the Software without restriction, including | ||||||||||||||||||||||||||||||||||
* without limitation the rights to use, copy, modify, merge, publish, | * without limitation the rights to use, copy, modify, merge, publish, | ||||||||||||||||||||||||||||||||||
* distribute, sublicense, and/or sell copies of the Software, and to | * distribute, sublicense, and/or sell copies of the Software, and to | ||||||||||||||||||||||||||||||||||
* permit persons to whom the Software is furnished to do so, subject to | * permit persons to whom the Software is furnished to do so, subject to | ||||||||||||||||||||||||||||||||||
* the following conditions: | * the following conditions: | ||||||||||||||||||||||||||||||||||
Show All 12 Lines | |||||||||||||||||||||||||||||||||||
/* | /* | ||||||||||||||||||||||||||||||||||
* pseudorandom number generator | * pseudorandom number generator | ||||||||||||||||||||||||||||||||||
*/ | */ | ||||||||||||||||||||||||||||||||||
#ifndef INCLUDED_RAND | #ifndef INCLUDED_RAND | ||||||||||||||||||||||||||||||||||
#define INCLUDED_RAND | #define INCLUDED_RAND | ||||||||||||||||||||||||||||||||||
#include <random> | |||||||||||||||||||||||||||||||||||
#include <cstdint> | |||||||||||||||||||||||||||||||||||
vladislavbelov: `<cstdint>` instread. | |||||||||||||||||||||||||||||||||||
/** | /** | ||||||||||||||||||||||||||||||||||
* return random integer in [min, max). | * return random integer in [min, max). | ||||||||||||||||||||||||||||||||||
* avoids several common pitfalls; see discussion at | * avoids several common pitfalls; see discussion at | ||||||||||||||||||||||||||||||||||
* http://www.azillionmonkeys.com/qed/random.html | * http://www.azillionmonkeys.com/qed/random.html | ||||||||||||||||||||||||||||||||||
**/ | **/ | ||||||||||||||||||||||||||||||||||
LIB_API size_t rand(size_t min_inclusive, size_t max_exclusive); | LIB_API size_t rand(size_t min_inclusive, size_t max_exclusive); | ||||||||||||||||||||||||||||||||||
/** | |||||||||||||||||||||||||||||||||||
* Low randomness, quite-a-lot-faster-than-std::mt19937 random number generator. | |||||||||||||||||||||||||||||||||||
* It matches the interface of UniformRandomBitGenerator for use in std::shuffle. | |||||||||||||||||||||||||||||||||||
Done Inline ActionsIt doesn't make sense to write numbers here, because they really depend on platform. vladislavbelov: It doesn't make sense to write numbers here, because they really depend on platform. | |||||||||||||||||||||||||||||||||||
*/ | |||||||||||||||||||||||||||||||||||
class CFastRand | |||||||||||||||||||||||||||||||||||
{ | |||||||||||||||||||||||||||||||||||
Not Done Inline ActionsDo we need that random class at all? std::mt19337 is fast enough in absolute numbers. vladislavbelov: Do we need that random class at all? `std::mt19337` is fast enough in absolute numbers. | |||||||||||||||||||||||||||||||||||
Not Done Inline Actions
Stan: >>! In D1584#74412, @Stan wrote:
> Static rng;
>
> | Compiler… | |||||||||||||||||||||||||||||||||||
Not Done Inline ActionsSounds like a premature optimization. We use std::mt19937 for generation of variations. And we don't see that places in profiler. vladislavbelov: Sounds like a premature optimization. We use `std::mt19937` for generation of variations. And… | |||||||||||||||||||||||||||||||||||
Done Inline ActionsWe do call this relatively often in the sound, compared to actor variations. I don't necessarily disagree, but it's not a very _costly_ optimisation either in terms of maintenance or code complexity, so I'd be inclined to go with it. wraitii: We do call this relatively often in the sound, compared to actor variations. I don't… | |||||||||||||||||||||||||||||||||||
public: | |||||||||||||||||||||||||||||||||||
using result_type = u32; | |||||||||||||||||||||||||||||||||||
constexpr static result_type min() { return 0; } | |||||||||||||||||||||||||||||||||||
constexpr static result_type max() { return 0xFFFF; } | |||||||||||||||||||||||||||||||||||
static result_type Rand(result_type& seed) | |||||||||||||||||||||||||||||||||||
{ | |||||||||||||||||||||||||||||||||||
// This is a mixed linear congruential random number generator. | |||||||||||||||||||||||||||||||||||
// The magic numbers are chosen so that they generate pseudo random numbers over a big enough period (0xFFFF). | |||||||||||||||||||||||||||||||||||
seed = 214013 * seed + 2531011; | |||||||||||||||||||||||||||||||||||
return (seed >> 16) & max(); | |||||||||||||||||||||||||||||||||||
} | |||||||||||||||||||||||||||||||||||
static float RandFloat(result_type& seed, float min, float max) | |||||||||||||||||||||||||||||||||||
{ | |||||||||||||||||||||||||||||||||||
return (static_cast<float>(Rand(seed)) / (0xFFFF)) * (max - min) + min; | |||||||||||||||||||||||||||||||||||
} | |||||||||||||||||||||||||||||||||||
CFastRand() {}; | |||||||||||||||||||||||||||||||||||
CFastRand(result_type init) : m_Seed(init) {}; | |||||||||||||||||||||||||||||||||||
result_type operator()() | |||||||||||||||||||||||||||||||||||
{ | |||||||||||||||||||||||||||||||||||
return Rand(m_Seed); | |||||||||||||||||||||||||||||||||||
} | |||||||||||||||||||||||||||||||||||
result_type m_Seed; | |||||||||||||||||||||||||||||||||||
}; | |||||||||||||||||||||||||||||||||||
#endif // #ifndef INCLUDED_RAND | #endif // #ifndef INCLUDED_RAND |
Wildfire Games · Phabricator
<cstdint> instread.