Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to use mt19937 in C++

2 Answers

0 votes
#include <iostream>
#include <random>

int main()
{
    std::mt19937 mt(time(nullptr));

    for (int i = 0; i < 10; i++)
        std::cout << mt() << '\n';
}




/*
run:

442545623
791260776
1254660388
2910229856
3672212806
771503738
904474238
1116529767
1458365050
4278033339

*/

 



answered Jan 7, 2023 by avibootz
0 votes
#include <iostream>
#include <random>

static std::random_device rd; 
static std::mt19937 seed{rd()}; 

int getRandom(int x, int y) {
    static std::uniform_int_distribution<int> uid(x, y); 
    
    return uid(seed); 
}

int main()
{
    for (int i = 0; i < 10; i++)
        std::cout << getRandom(1, 15) << " ";   
}




/*
run:

14 1 11 3 7 2 9 6 8 12 

*/

 



answered Jan 7, 2023 by avibootz

Related questions

1 answer 115 views
115 views asked Jan 6, 2023 by avibootz
1 answer 124 views
124 views asked Jan 7, 2023 by avibootz
...