c++ 随机

Singleton.h

 1 #pragma once
 2 #include <cassert>
 3 
 4 
 5 template <class T>
 6 class Singleton
 7 {
 8 public:
 9     Singleton()
10     {
11         assert(!ms_pSingleton);
12         ms_pSingleton = static_cast<T*>(this);
13     }
14 
15     ~Singleton()
16     {
17         assert(ms_pSingleton);
18         ms_pSingleton = nullptr;
19     }
20 
21     static T* Instance()
22     {
23         return ms_pSingleton;
24     }
25 
26 private:
27     Singleton(const Singleton<T> &) = delete;
28     Singleton& operator = (const Singleton<T> &) = delete;
29 protected:
30     static T *ms_pSingleton;
31 };
32 
33 template <class T>
34 T *Singleton<T>::ms_pSingleton = nullptr;

Random.h

 1 #pragma once
 2 #include <random>
 3 #include <map>
 4 
 5 #include "Singleton.h"
 6 
 7 struct RandomCreater
 8 {
 9     long randomSeed();
10     void init();
11     int random(int begin, int end);
12 
13     std::mt19937 *mt = nullptr;
14     int useCount = 0;
15     long seed = 0;
16 };
17 
18 
19 class Random : public Singleton<Random>
20 {
21 public:
22     Random();
23     ~Random();
24     int random(int begin, int end, std::string createName = "");
25 private:
26     std::map<std::string, std::shared_ptr<RandomCreater>> m_allRandomCreater;
27     std::shared_ptr<RandomCreater> m_defaultCreater;
28 };

Random.cpp

 1 #include "Random.h"
 2 
 3 long RandomCreater::randomSeed()
 4 {
 5     std::random_device rd;
 6     return rd();
 7 }
 8 
 9 void RandomCreater::init()
10 {
11     if (seed == 0 || useCount > 1000)
12     {
13         seed = randomSeed();
14         useCount = 0;
15         if (mt != nullptr)
16             delete mt;
17 
18         mt = new std::mt19937(seed);
19     }
20 }
21 
22 int RandomCreater::random(int begin, int end)
23 {
24     if (begin == end) return begin;
25 
26     int min = begin < end ? begin : end;
27     int max = begin < end ? end : begin;
28 
29     int rt = (*mt)() % (abs(max - min));
30     rt = rt + min;
31 
32     return rt;
33 }
34 
35 
36 Random::Random()
37 {
38     m_defaultCreater = std::make_shared<RandomCreater>();
39     m_defaultCreater->init();
40 }
41 
42 Random::~Random()
43 {
44 }
45 
46 int Random::random(int begin, int end, std::string createName)
47 {
48     std::shared_ptr<RandomCreater> useCreater = m_defaultCreater;
49     if (!createName.empty())
50     {
51         if (m_allRandomCreater.find(createName) == m_allRandomCreater.end())
52         {
53             m_allRandomCreater[createName] = std::make_shared<RandomCreater>();
54         }
55 
56         useCreater = m_allRandomCreater[createName];
57     }
58     useCreater->init();
59     useCreater->useCount++;
60     return useCreater->random(begin, end);
61 }

main.cpp

 1 #include <iostream>
 2 #include "Random.h"
 3 
 4 int main(int argc, char* argv[])
 5 {
 6     new Random();
 7     std::cout << Random::Instance()->random(-100, 100);
 8 
 9     getchar();
10 
11     return 0;
12 }

猜你喜欢

转载自www.cnblogs.com/weishuan/p/10533233.html