1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include "seal/randomgen.h"
#include <cstdint>
#include <limits>
#include <memory>
#include <stdexcept>
namespace seal
{
/**
A simple wrapper class to implement C++ UniformRandomBitGenerator type properties
for a given polymorphic UniformRandomGenerator instance. The resulting object can
be used as a randomness source in C++ standard random number distribution classes,
such as std::uniform_int_distribution, std::normal_distribution, or any of the
standard RandomNumberEngine classes.
*/
class RandomToStandardAdapter
{
public:
using result_type = std::uint32_t;
/**
Creates a new RandomToStandardAdapter backed by a given UniformRandomGenerator.
@param[in] generator A backing UniformRandomGenerator instance
@throws std::invalid_argument if generator is null
*/
RandomToStandardAdapter(std::shared_ptr<UniformRandomGenerator> generator) : generator_(generator)
{
if (!generator_)
{
throw std::invalid_argument("generator cannot be null");
}
}
/**
Returns a new random number from the backing UniformRandomGenerator.
*/
SEAL_NODISCARD inline result_type operator()()
{
return generator_->generate();
}
/**
Returns the backing UniformRandomGenerator.
*/
SEAL_NODISCARD inline auto generator() const noexcept
{
return generator_;
}
/**
Returns the smallest possible output value.
*/
SEAL_NODISCARD inline static constexpr result_type min() noexcept
{
return std::numeric_limits<result_type>::min();
}
/**
Returns the largest possible output value.
*/
SEAL_NODISCARD static constexpr result_type max() noexcept
{
return std::numeric_limits<result_type>::max();
}
private:
std::shared_ptr<UniformRandomGenerator> generator_;
};
} // namespace seal