tools
読み取り中…
検索中…
一致する文字列を見つけられません
random.h
[詳解]
1#pragma once
2#include <random>
3#include <chrono>
4#include <sstream>
5
6/**
7 * @file random.h
8 * @brief 乱数生成ユーティリティ
9 *
10 * メルセンヌツイスタ (MT19937) を使用したランダム整数生成と、
11 * 時刻ベースのユニークなシードID生成を提供します。
12 */
13
14/**
15 * @brief 指定範囲内のランダムな整数を返す
16 * @param low 生成する乱数の最小値(含む)
17 * @param up 生成する乱数の最大値(含む)
18 * @return [low, up] の範囲内のランダムな整数
19 */
20inline int randomNum(const int& low, const int& up) {
21 std::random_device rd;
22 std::mt19937 gen(rd());
23 std::uniform_int_distribution<int> distribution(low, up);
24 return distribution(gen);
25}
26
27/**
28 * @brief 時刻と乱数を組み合わせたユニークなシード文字列を生成する
29 * @return MT19937-64 の乱数値と高精度タイムスタンプを連結した文字列
30 * @note セッションをまたいで衝突しにくいユニークIDが必要な場面に利用できます
31 */
32inline std::string randomSeed() {
33 std::random_device rd;
34 std::mt19937_64 eng(rd());
35 auto now = std::chrono::high_resolution_clock::now().time_since_epoch().count();
36 std::stringstream ss;
37 ss << eng() << now;
38 return ss.str();
39}
std::string randomSeed()
時刻と乱数を組み合わせたユニークなシード文字列を生成する
Definition random.h:32
int randomNum(const int &low, const int &up)
指定範囲内のランダムな整数を返す
Definition random.h:20