C++11 random è¦ãæ¸ã
C++11以éã§ã¯<random>ヘッダã§è¯è³ªãªæ¬ä¼¼ä¹±æ°ãå¾ããã¨ãã§ããã
#include <random>
å¤é¨ããã®ä¹±æ°
å¤é¨ããä¹±æ°ãå¾ãã«ã¯random_deviceã使ãã
#include <random> #include <iostream> int main() { std::random_device rand_dev; std::cout << rand_dev() << std::endl; std::cout << rand_dev() << std::endl; return 0; }
ä¸ã®ããã«ãå¤é¨ããã®ä¹±æ°æºã¯é¢æ°ã¨ãã¦å¼ã³åºããã¨ã§ä¹±æ°ãçæããã
ãã®ããã°ã©ã ã¯å®è¡ãããã¨ã«ç°ãªãå¤ãåºåããã
æ¬ä¼¼ä¹±æ°
æ¬ä¼¼ä¹±æ°ã¯ãã·ã¼ãå¤ãæ¾ãè¾¼ãã¨ä¹±æ°ã£ã½ãæ°åãåãåºãããã°ã©ã ã
çææ¹æ³ãä½ç¨®é¡ãããããmt19937ã ãææ¡ãã¦ããã°ããã
#include <random> #include <iostream> int main() { std::mt19937 rand_src(12345); std::cout << rand_src() << std::endl; std::cout << rand_src() << std::endl; return 0; }
ä¸ã®ããã«ãæ¬ä¼¼ä¹±æ°çæå¨ã¯é¢æ°ã¨ãã¦å¼ã³åºããã¨ã§æ¬ä¼¼ä¹±æ°ãçæããã
ãã®ããã°ã©ã ã¯ç°ãªãæ´æ°ã2ã¤åºåããããããã°ã©ã ã®å®è¡ãã¨ã«åãæ´æ°ãåºåããã
ã½ã¼ã¹ä¸ã®12345ãã·ã¼ãå¤ã§ããããã®æ°å¤ãå¤æ´ããã¨ãç°ãªãæ´æ°ãåºåããããã«ãªãã
ä¹±æ°ã®å å·¥
ä¸ã§çæããä¹±æ°ã¯32bitä¸æ§ä¹±æ°çãªã®ã§ãå¿ è¦ãªåå¸ãåºãããã«å å·¥ããã
#include <random> #include <iostream> int main() { std::mt19937 rand_src(12345); std::uniform_int_distribution<int> rand_dist(0, 99); std::cout << rand_dist(rand_src) << std::endl; std::cout << rand_dist(rand_src) << std::endl; return 0; }
ä¸ã®ããã«ãåå¸ãé¢æ°ã¨ãã¦ä½¿ãã¨ãã¯ãå¼æ°ã«ä¹±æ°æºã¸ã®åç §ã渡ããå é¨ã§ä¹±æ°æºã(å¿ è¦ã«å¿ãã¦è¤æ°å)å¼ã°ãã¦ããã
ä¸ã®ã½ã¼ã¹ã³ã¼ãã§ã¯ãMTã§çæãããæ¬ä¼¼ä¹±æ°åããã¨ã«ãä¸æ§åå¸ã«å¾ã0以ä¸99以ä¸ã®æ´æ°ã2ã¤çæãã¦ããã
ãã使ãåå¸:
- é¢æ£ä¸æ§åå¸ uniform_int_distribution
- ãã³ãã¬ã¼ãå¼æ° : æ»ãå¤ã®å(intç)
- ã³ã³ã¹ãã©ã¯ã¿ã®å¼æ° : a以ä¸b以ä¸ã®ä¹±æ°ãçæ
- é£ç¶ä¸æ§åå¸ uniform_real_distribution
- ãã³ãã¬ã¼ãå¼æ° : æ»ãå¤ã®å(doubleç)
- ã³ã³ã¹ãã©ã¯ã¿ã®å¼æ° : a以ä¸bæªæºã®ä¹±æ°ãçæ
- æ£è¦åå¸ normal_distribution
- ãã³ãã¬ã¼ãå¼æ° : æ»ãå¤ã®å(doubleç)
- ã³ã³ã¹ãã©ã¯ã¿ã®å¼æ° : å¹³åmeanã§æ¨æºåå·®stddev (çç¥ããã¨æ¨æºæ£è¦åå¸)
æå·è«çæ¬ä¼¼ä¹±æ°
ãã¶ããã®ã©ã¤ãã©ãªã«ã¯ç¡ãã
åå¸ã¨æ¬ä¼¼ä¹±æ°çæå¨ãåä½ããã
åå¸ã«æ¯åæ¬ä¼¼ä¹±æ°çæå¨ãã¤ããã®ã¯é¢åã§ããããã<functional>ヘッダã«ããbindã¨refã使ã£ã¦ä»¥ä¸ã®ããã«ããã
#include <random> #include <iostream> #include <functional> int main() { std::mt19937 rand_src(12345); std::uniform_int_distribution<int> rand_dist(0, 9); auto rand_dist_bound = std::bind(rand_dist, std::ref(rand_src)); std::cout << rand_dist_bound() << std::endl; std::cout << rand_dist_bound() << std::endl; return 0; }
refã使ããã«ã
auto rand_dist_bound = std::bind(rand_dist, rand_src);
ã¨ãã¦ãä¸å¿åä½ããããããã¯æ¬ä¼¼ä¹±æ°çæå¨ã®ã³ãã¼ãä½æãã¦ãã¾ãã®ã§æå³ãå¤ãããåãæ¬ä¼¼ä¹±æ°çæå¨ãè¤æ°ã®åå¸ã§ä½¿ãå ´åã¯æ³¨æãå¿ è¦(é常ãrefãã¤ããã»ãã好ã¾ãã)
ããã¯ä»¥ä¸ã®ããã«ãã¦ãåããã¨ã§ãã:
#include <random> #include <iostream> #include <functional> int main() { std::mt19937 rand_src(12345); auto rand_dist_bound = std::bind( std::uniform_int_distribution<int>(0, 9), std::ref(rand_src)); std::cout << rand_dist_bound() << std::endl; std::cout << rand_dist_bound() << std::endl; return 0; }
ããã«ããã®æ¬ä¼¼ä¹±æ°çæå¨ã¯ãã®åå¸ã§ãã使ããªãã¨ããã®ã§ããã°ã以ä¸ã®ããã«ãã§ãã(ãã®å ´åã¯refã¯ä½¿ããªã):
#include <random> #include <iostream> #include <functional> int main() { auto rand_dist_bound = std::bind( std::uniform_int_distribution<int>(0, 9), std::mt19937(12345)); std::cout << rand_dist_bound() << std::endl; std::cout << rand_dist_bound() << std::endl; return 0; }
ã·ã¼ãã®æ±ºãæ¹
ã·ã¼ãã®æ±ºãæ¹ã¯å ´åã«ããã大éæã«è¨ãã¨æ¬¡ã®2種é¡:
- å¤é¨ããã®ä¹±æ°ã«åºã¥ãã¦æ±ºãã
- 決ãæã¡
ãã ãã以ä¸ã®ãã¨ã«æ³¨æãå¿ è¦ã :
- å¤é¨ããã®ä¹±æ°ãå¿ è¦ãªç¶æ³ã¯ãã¾ãå¤ããªã : ä¹±æ°ã«åºã¥ãã¢ã«ã´ãªãºã ã¯ãã·ã¼ã決ãæã¡ã®æ¬ä¼¼ä¹±æ°ã§åé¡ãªãåä½ããã
- ããããåç¾æ§ãèããã¨ãã·ã¼ãã決ãæã¡ã«ããã»ããè¯ãã
- ãå®è¡ãã¨ã«åä½ãå¤ããããã¨ããéè¦ã¯ãã³ãã³ãã©ã¤ã³å¼æ°ãªã©ã§ã·ã¼ããæå®ã§ããããã«ããã ãã§æºè¶³ããããã¨ãããã
å¤é¨ããã®ä¹±æ°ã«åºã¥ãã¦ã·ã¼ãã決ãã
å¤é¨ããã®ä¹±æ°ã«åºã¥ãã¦ã·ã¼ãã決ããã«ã¯ãåã«ä»¥ä¸ã®ããã«ããã°ãã:
#include <random> #include <iostream> int main() { std::random_device rand_dev; std::mt19937 rand_src(rand_dev()); std::cout << rand_src() << std::endl; std::cout << rand_src() << std::endl; return 0; }
ãã®ããã«ããã¨ãæ¯åã·ã¼ããå¤ããã®ã§ãå®è¡ãããã¨ã«ç°ãªãæ´æ°ã表示ãããã
å¤é¨ããã®ä¹±æ°æºã1åãã使ã£ã¦ããªãã®ã§ã以ä¸ã®ããã«ãã§ãã:
#include <random> #include <iostream> int main() { std::mt19937 rand_src(std::random_device{}()); std::cout << rand_src() << std::endl; std::cout << rand_src() << std::endl; return 0; }
ã³ãã³ãã©ã¤ã³å¼æ°ã«åºã¥ãã¦ã·ã¼ãã決ãã
ä¾ãã°ãã³ãã³ãã©ã¤ã³å¼æ°ã®1çªç®ãããå ´åã¯ãããã·ã¼ãã¨ãã¦ä½¿ãããã«ããã«ã¯:
#include <random> #include <iostream> #include <string> int main(int argc, char *argv[]) { int seed = 12345; if(argc > 1) { seed = std::stoi(argv[1]); } std::mt19937 rand_src(seed); std::cout << rand_src() << std::endl; std::cout << rand_src() << std::endl; return 0; }
ã¾ããã³ãã³ãã©ã¤ã³å¼æ°ãä¸ããªãå ´åã®ããã©ã«ãã¯ãå¤é¨ããã®ä¹±æ°ã§ããããããããªã:
#include <random> #include <iostream> #include <string> int main(int argc, char *argv[]) { int seed; if(argc > 1) { seed = std::stoi(argv[1]); } else { seed = std::random_device{}(); } std::mt19937 rand_src(seed); std::cout << rand_src() << std::endl; std::cout << rand_src() << std::endl; return 0; }
ãµã³ãã«ã³ã¼ã
POSIX usleep(3)ã使ã£ã¦ã1ç§éãããå¹³å5åã®ã©ã³ãã ãª(ãã¢ã½ã³éç¨ã«åºã¥ã)ã¤ãã³ããçºçããã (ååããããã®ç²åãããã¨ãã®æ¾å°ç·ã®çºçã¨ãããã)
#include <random> #include <iostream> #include <string> #include <functional> #include <unistd.h> int main(int argc, char *argv[]) { int seed; if(argc > 1) { seed = std::stoi(argv[1]); } else { seed = std::random_device{}(); } std::mt19937 rand_src(seed); auto rand = std::bind( std::exponential_distribution<double>(5.0 / 1000000.0), std::ref(rand_src)); while(true) { std::cout << "a" << std::flush; usleep(rand()); } std::cout << rand_src() << std::endl; std::cout << rand_src() << std::endl; return 0; }