Ice 3.7 C++98 API Reference
Loading...
Searching...
No Matches
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
18namespace IceUtilInternal
19{
20
21ICE_API void generateRandom(char*, size_t);
22ICE_API unsigned int random(int = 0);
23
24#ifdef ICE_CPP11_COMPILER
25
26template<class T>
27void 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
36struct 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
44template<class T>
45void shuffle(T first, T last)
46{
47 RandomNumberGenerator rng;
48 random_shuffle(first, last, rng);
49}
50
51#endif
52
53}
54
55#endif
#define ICE_API
Definition Config.h:197