samedi 1 octobre 2016

Is it generally accepted to implement your own random functions ? (C++)

Suppose you are working on a (serious) project that requires random numbers.

Lines like:

int num = rand() % 6 + 5; //random number between 5 - 10

are (obviously) not going to appear there. The C++11 header <random> beats this old C-Style without any doubts.

std::random_device rd;
std::mt19937 engine(rd());
std::uniform_int_distribution<int> generate(5, 10);
int num = generate(engine);

But when working in a group, I can see 2 problems with this methode:

  • The Syntax is wierd and hard to read.

  • It's too complex for such an often simple task.

So it would be really nice to have a function (based on this methode) looking like this:

int getRandom(int range_begin, int range_end);

As far as i'm concerned, there is no such STL function. So I'm asking for your experience: would it be ok to implement this function on your own? Maybe export into a seperate header, then overload it for doubles and float etc. I am asking this question, because in this modern C++ time, where it's standard to use STL functions, this is a situation where the syntax actually forces you to implement on our own! Thanks for your help & experience on this topic.




Aucun commentaire:

Enregistrer un commentaire