CARLA
 
载入中...
搜索中...
未找到
Random.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 <carla/geom/Location.h>// 引入 Carla 库中的 Location 类
10
11#include <algorithm>// 提供算法操作
12#include <random> // 提供随机数生成的功能
13
14namespace util {
15// 随机数工具类,封装了常见的随机数生成方法
16 class Random {
17 public:
18// 生成范围 [min, max) 内的随机双精度浮点数
19 static double Uniform(double min, double max) {
20 std::uniform_real_distribution<double> distribution(min, max); // 定义一个随机浮点数分布
21 return distribution(_engine); // 使用随机引擎生成并返回分布中的随机数
22 }
23// 生成坐标范围 [min, max) 内的随机 carla::geom::Location 对象
24 static carla::geom::Location Location(float min, float max) {
25 std::uniform_real_distribution<float> distribution(min, max); // 定义一个随机浮点数分布
26 return {distribution(_engine), distribution(_engine), distribution(_engine)};// 创建一个随机的 Location 对象,包含随机的 x, y, z 坐标值
27 }
28// 使用随机引擎对提供的范围进行洗牌
29 // 这个函数会将给定范围内的元素顺序随机打乱
30 template <typename RangeT>
31 static void Shuffle(RangeT &range) {
32 std::shuffle(std::begin(range), std::end(range), _engine); // 使用 _engine 对 range 中的元素顺序进行洗牌
33 }
34 private:
35 // 线程局部随机数引擎,确保每个线程都有自己的实例
36 // Defined in test.cpp.
37// 在 test.cpp 中定义,以便进行适当的初始化
38 static thread_local std::mt19937_64 _engine; // 使用 64 位的 Mersenne Twister 随机数生成器
39 };
40
41} // namespace util
double min(double v1, double v2)
返回两个数中的较小值
Definition Simplify.h:591
static double Uniform(double min, double max)
Definition Random.h:19
static void Shuffle(RangeT &range)
Definition Random.h:31
static thread_local std::mt19937_64 _engine
Definition Random.h:38
static carla::geom::Location Location(float min, float max)
Definition Random.h:24