c++ 随机数 取值范围 多线程
https://www.cnblogs.com/exciting/p/11162855.html
#include <random> std::random_device rd; // A function object for generating seeds std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(1, 6000);//取值 1-6000
使用:
dis(gen)
这样写随机数发现,多次运行代码,每次生成的随机数都一样(在dev c++ windows环境下)。需要改成:
// random values std::random_device seeder; auto seed=seeder.entropy()?seeder():time(nullptr); std::mt19937 engine( static_cast<mt19937::result_type>(seed)); std::uniform_int_distribution<int> uniformDist(0, 100); // 这样取值,才每次运行程序值都会变 uniformDist(engine);
线程延迟随机多少秒内启动:
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h> #include <thread> #include <random> using namespace std; using std::cout; class HelloWorld { public: HelloWorld(string url) { std::random_device rd; // A function object for generating seeds std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(1, 6000);// const int len = 100; std::thread threads[len]; std::cout << "Spawning 5 threads...\n"; for (int i = 0; i < len; i++) { threads[i] = std::thread(&HelloWorld::thread_task, this, url, dis(gen)); } std::cout << "Done spawning threads! Now wait for them to join\n"; for (auto& t : threads) { t.join(); } std::cout << "All threads joined.\n"; } void thread_task(string url, int time) { std::this_thread::sleep_for(std::chrono::milliseconds(time)); std::cout << "hello thread " << std::this_thread::get_id() << " paused " << time << " milliseconds" << std::endl; } }; int main() { HelloWorld hw("url"); return EXIT_SUCCESS; }
输入任意, 不停的加入新线程:
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h> #include <thread> #include <random> using namespace std; using std::cout; const int len = 10; std::vector<std::thread> threads(10); class HelloWorld { public: HelloWorld(string url) { std::random_device rd; // A function object for generating seeds std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(1, 6000);// std::cout << "Spawning 5 threads...\n"; for (int i = 0; i < len; i++) { threads.push_back( std::thread(&HelloWorld::thread_task, this, url, dis(gen))); } /* std::cout << "Done spawning threads! Now wait for them to join\n"; for (auto& t : threads) { t.join(); } std::cout << "All threads joined.\n"; */ } void thread_task(string url, int time) { std::this_thread::sleep_for(std::chrono::milliseconds(time)); std::cout << "hello thread " << std::this_thread::get_id() << " paused " << time << " milliseconds" << std::endl; } }; int main() { //输入一个键就不停的循环加入 char c = 'a'; while (c != 'q') { HelloWorld hw("url"); cout << "loop:"; cin >> c; } return EXIT_SUCCESS; }
分类:
C/C++
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2015-08-05 软件安装
2013-08-05 X11,GTK,QT,GNOME的区别与联系(UI工具总结)
2013-08-05 Linux 常用命令——cat, tac, nl, more, less, head, tail, od
2013-08-05 Linux 常用命令——which, whereis, locate, find
2013-08-05 Linux 常用命令——df, du, ln 目录树 文件大小 查找大文件