Portál AbcLinuxu, 10. května 2025 02:25
rand
a random
, nejprve je však třeba nastavit počáteční hodnotu pomocí srand
nebo srandom
. Třeba takto:
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(0)); cout << "Náhodné číslo od nuly do deseti: " << rand() % 11 << endl; }
#include <cstdlib> int genrand(int min, int max) { return min+(rand()%(++max-min)); }Jinak viz rand(3).
srand( (unsigned)time( NULL ) );
//typ nahody boost::ecuyer1988 genType; //generator nahody s urcenim mezi boost::variate_generator<boost::ecuyer1988, boost::uniform_int<> > mujGenerator(genType, boost::uniform_int<>(dolniMez,horniMez)); //generovani hodnot for (int i = 0; i < 1000; ++i) cout << mujGenerator() << endl;
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.