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;
}

原文链接: https://www.cnblogs.com/bigben0123/p/13437838.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/201152

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午8:41
下一篇 2023年2月12日 下午8:41

相关推荐