can random C++ 2011 standard functions be called from a C routine? -
i need generate random integer range , have found interesting discussed here in answer @walter
. however, c++11
standard , need use c
, there way of making call c? reproduce answer here:
#include <random> std::random_device rd; // used once initialise (seed) engine std::mt19937 rng(rd()); // random-number engine used (mersenne-twister in case) std::uniform_int_distribution<int> uni(min,max); // guaranteed unbiased auto random_integer = uni(rng);
you cannot use c++ classes in c, can wrap c++ functionality in functions, can called c, e.g. in getrandom.cxx:
#include <random> static std::random_device rd; static std::mt19937 rng(rd()); static std::uniform_int_distribution<int> uni(min,max); extern "c" int get_random() { return uni(rng); }
this c++ module exporting get_random function c linkage (that is, callable c. declare in getrandom.h:
extern "c" int get_random();
and can call c code.
Comments
Post a Comment