SFML community forums

General => General discussions => Topic started by: Isamu on August 26, 2014, 02:33:23 am

Title: Randomizer.hpp
Post by: Isamu on August 26, 2014, 02:33:23 am
What was the reason of removing Randomizer.hpp from the sfml package?
Title: Re: Randomizer.hpp
Post by: FRex on August 26, 2014, 03:00:59 am
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.
Title: Re: Randomizer.hpp
Post by: Sub on August 26, 2014, 05:17:58 am
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);
Title: Re: Randomizer.hpp
Post by: Jesper Juhl on August 26, 2014, 08:25:07 am
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


Title: Re: Randomizer.hpp
Post by: SeriousITGuy on August 26, 2014, 09:07:26 am
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.  ;)
Title: Re: Randomizer.hpp
Post by: Nexus on August 26, 2014, 09:54:14 am
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.
Title: Re: Randomizer.hpp
Post by: paupav on August 27, 2014, 11:55:28 pm
what's wrong with std::rand() ?
Title: Re: Randomizer.hpp
Post by: Hapax on August 28, 2014, 12:08:31 am
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?
Title: Re: Randomizer.hpp
Post by: FRex on August 28, 2014, 12:13:50 am