Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Randomizer.hpp  (Read 5389 times)

0 Members and 1 Guest are viewing this topic.

Isamu

  • Newbie
  • *
  • Posts: 9
  • Hello, you can call me Isamu.
    • View Profile
    • QLogie
Randomizer.hpp
« on: August 26, 2014, 02:33:23 am »
What was the reason of removing Randomizer.hpp from the sfml package?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Randomizer.hpp
« Reply #1 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.
Back to C++ gamedev with SFML in May 2023

Sub

  • Full Member
  • ***
  • Posts: 159
    • View Profile
Re: Randomizer.hpp
« Reply #2 on: August 26, 2014, 05:17:58 am »
Taken from stack overflow
--
#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);

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Randomizer.hpp
« Reply #3 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


« Last Edit: August 26, 2014, 09:21:24 am by Jesper Juhl »

SeriousITGuy

  • Full Member
  • ***
  • Posts: 123
  • Still learning...
    • View Profile
Re: Randomizer.hpp
« Reply #4 on: August 26, 2014, 09:07:26 am »
Taken from stack overflow
--
#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.  ;)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Randomizer.hpp
« Reply #5 on: August 26, 2014, 09:54:14 am »
Thor also provides global random functions 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

paupav

  • Full Member
  • ***
  • Posts: 156
    • View Profile
    • Email
Re: Randomizer.hpp
« Reply #6 on: August 27, 2014, 11:55:28 pm »
what's wrong with std::rand() ?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Randomizer.hpp
« Reply #7 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?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Randomizer.hpp
« Reply #8 on: August 28, 2014, 12:13:50 am »
  • 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.
Back to C++ gamedev with SFML in May 2023

 

anything