CARLA
 
载入中...
搜索中...
未找到
RandomEngine.h
浏览该文件的文档.
1// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
9#include <random>
10
11#include "RandomEngine.generated.h"
12
13UCLASS(Blueprintable,BlueprintType)
14class CARLA_API URandomEngine : public UObject
15{
16 GENERATED_BODY()
17
18public:
19
20 // ===========================================================================
21 /// @name Generate Ids
22 // ===========================================================================
23 /// @{
24
25 /// Generate a non-deterministic random id.
26 static uint64 GenerateRandomId();
27
28 /// @}
29 // ===========================================================================
30 /// @name Seed
31 // ===========================================================================
32 /// @{
33
34 /// Generate a non-deterministic random seed.
35 UFUNCTION(BlueprintCallable)
36 static int32 GenerateRandomSeed();
37
38 /// Generate a seed derived from previous seed.
39 UFUNCTION(BlueprintCallable)
40 int32 GenerateSeed();
41
42 /// Seed the random engine.
43 UFUNCTION(BlueprintCallable)
44 void Seed(int32 InSeed)
45 {
46 Engine.seed(InSeed);
47 }
48
49 /// @}
50 // ===========================================================================
51 /// @name Uniform distribution
52 // ===========================================================================
53 /// @{
54
55 UFUNCTION(BlueprintCallable)
56 float GetUniformFloat()
57 {
58 return std::uniform_real_distribution<float>()(Engine);
59 }
60
61 UFUNCTION(BlueprintCallable)
62 float GetUniformFloatInRange(float Minimum, float Maximum)
63 {
64 return std::uniform_real_distribution<float>(Minimum, Maximum)(Engine);
65 }
66
67 UFUNCTION(BlueprintCallable)
68 int32 GetUniformIntInRange(int32 Minimum, int32 Maximum)
69 {
70 return std::uniform_int_distribution<int32>(Minimum, Maximum)(Engine);
71 }
72
73 UFUNCTION(BlueprintCallable)
74 bool GetUniformBool()
75 {
76 return (GetUniformIntInRange(0, 1) == 1);
77 }
78
79 /// @}
80 // ===========================================================================
81 /// @name Other distributions
82 // ===========================================================================
83 /// @{
84
85 UFUNCTION(BlueprintCallable)
86 bool GetBernoulliDistribution(float P)
87 {
88 return std::bernoulli_distribution(P)(Engine);
89 }
90
91 UFUNCTION(BlueprintCallable)
92 int32 GetBinomialDistribution(int32 T, float P)
93 {
94 return std::binomial_distribution<int32>(T, P)(Engine);
95 }
96
97 UFUNCTION(BlueprintCallable)
98 int32 GetPoissonDistribution(float Mean)
99 {
100 return std::poisson_distribution<int32>(Mean)(Engine);
101 }
102
103 UFUNCTION(BlueprintCallable)
104 float GetExponentialDistribution(float Lambda)
105 {
106 return std::exponential_distribution<float>(Lambda)(Engine);
107 }
108
109 UFUNCTION(BlueprintCallable)
110 float GetNormalDistribution(float Mean, float StandardDeviation)
111 {
112 return std::normal_distribution<float>(Mean, StandardDeviation)(Engine);
113 }
114
115 /// @}
116 // ===========================================================================
117 /// @name Sampling distributions
118 // ===========================================================================
119 /// @{
120
121 UFUNCTION(BlueprintCallable)
122 bool GetBoolWithWeight(float Weight)
123 {
124 return (Weight >= GetUniformFloat());
125 }
126
127 UFUNCTION(BlueprintCallable)
128 int32 GetIntWithWeight(const TArray<float> &Weights)
129 {
130 return std::discrete_distribution<int32>(
131 Weights.GetData(),
132 Weights.GetData() + Weights.Num())(Engine);
133 }
134
135 /// @}
136 // ===========================================================================
137 /// @name Elements in TArray
138 // ===========================================================================
139 /// @{
140
141 template <typename T>
142 auto &PickOne(const TArray<T> &Array)
143 {
144 check(Array.Num() > 0);
145 return Array[GetUniformIntInRange(0, Array.Num() - 1)];
146 }
147
148 template <typename T>
149 void Shuffle(TArray<T> &Array)
150 {
151 std::shuffle(Array.GetData(), Array.GetData() + Array.Num(), Engine);
152 }
153
154 /// @}
155
156private:
157
158 std::minstd_rand Engine;
159};
std::minstd_rand Engine
void Shuffle(TArray< T > &Array)
auto & PickOne(const TArray< T > &Array)