SFML community forums
General => General discussions => Topic started by: Isamu on August 26, 2014, 02:33:23 am
-
What was the reason of removing Randomizer.hpp from the sfml package?
-
It was nearly completely useless and possibly harmful.
It consisted of few super short functions, had a global value, used srand and rand anyway and had idiotically non uniform distribution.
Use real randomness library, C++11 <random> header or write the entire 10 lines of 'wrapper' that 'Randomizer' used to be for C functions yourself.
-
Taken from stack overflow (http://stackoverflow.com/questions/12657962/how-do-i-generate-a-random-number-between-two-variables-that-i-have-stored)
--
#include <random>
#include <chrono>
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 rng(seed);
std::uniform_int_distribution<int> gen(min, max); // uniform, unbiased
int r = gen(rng);
-
Just a few random ;) links that seem to be relevant here:
Pseudo-random number generation - http://en.cppreference.com/w/cpp/numeric/random
rand() Considered harmful - http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
Probability and Games: Damage rolls - http://www.redblobgames.com/articles/probability/damage-rolls.html
Understanding randomness in terms of mastery - http://www.lostgarden.com/2012/12/understanding-randomness-in-terms-of.html
-
Taken from stack overflow (http://stackoverflow.com/questions/12657962/how-do-i-generate-a-random-number-between-two-variables-that-i-have-stored)
--
#include <random>
#include <chrono>
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 rng(seed);
std::uniform_int_distribution<int> gen(min, max); // uniform, unbiased
int r = gen(rng);
I use this algorithm to generate random numbers, works like it should.
Made a few tests how good the distribution really is with 100 million numbers between 1 and 100. Good enough to (nearly) appear truly random.
Thanks Jesper for the additional links, some important and interesting info in there. ;)
-
Thor also provides global random functions (http://www.bromeon.ch/libraries/thor/v2.0/doc/_random_8hpp.html) that use C++11 engines and uniform distributions. Global state is not always wanted, but sometimes you just need a random number without caring about where it comes from.
-
what's wrong with std::rand() ?
-
what's wrong with std::rand() ?
rand() Considered harmful - http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
std::rand() is okay if you need to populate some data for testing rather than just sequential numbering, but other than that, why not use the STL stuff?
-
- It's global state.
- There is no (sane) guarantee on max number returned, it's specified in a macro and it just has to be at least 32767 which is pretty low. I think it's that on VC++, on 32 bit GCC it's max int.
- There are no guarantees on quality, period or safety of numbers.
- Algorithm is anything implementers pick, state doesn't mandate that, it and it's properties can vary wildly across implementations.
- SFML used a very naive way of getting random float or int range from it.