Ice 3.7 C++11 API Reference
Random.h
Go to the documentation of this file.
1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_UTIL_RANDOM_H
6 #define ICE_UTIL_RANDOM_H
7 
8 #include <IceUtil/Config.h>
9 #include <IceUtil/Exception.h>
10 
11 #ifdef ICE_CPP11_COMPILER
12 # include <algorithm>
13 # include <random>
14 #else
15 # include <functional>
16 #endif
17 
18 namespace IceUtilInternal
19 {
20 
21 ICE_API void generateRandom(char*, size_t);
22 ICE_API unsigned int random(int = 0);
23 
24 #ifdef ICE_CPP11_COMPILER
25 
26 template<class T>
27 void shuffle(T first, T last)
28 {
29  std::random_device rd;
30  std::mt19937 rng(rd());
31  std::shuffle(first, last, rng);
32 }
33 
34 #else
35 
36 struct RandomNumberGenerator : public std::unary_function<std::ptrdiff_t, std::ptrdiff_t>
37 {
38  std::ptrdiff_t operator()(std::ptrdiff_t d)
39  {
40  return static_cast<std::ptrdiff_t>(IceUtilInternal::random(static_cast<int>(d)));
41  }
42 };
43 
44 template<class T>
45 void shuffle(T first, T last)
46 {
47  RandomNumberGenerator rng;
48  random_shuffle(first, last, rng);
49 }
50 
51 #endif
52 
53 }
54 
55 #endif
ICE_API
#define ICE_API
Definition: Config.h:197
Exception.h
Config.h