乱数したい(G4Random::getTheSeed

1#include "Randomize.hh"
2
3long seed = G4Random::getTheSeed();
4// long seed = CLHEP::HepRandom::getTheSeed();
5
6G4debug << "Current seed: " << seed << G4endl;
7// Current seed: 3

G4Randomで乱数シードを操作できます。 読み込むヘッダーファイル名はRandomize.hhなので、注意が必要です。

注釈

G4RandomCLHEP::HepRandomのエイリアスです。 CLHEP::HepRandomを直接使う場合、Randomize.hhのインクルードは不要です。

乱数設定を保存/復元したい(G4Random::saveEngineStatus

1#include "Randomize.hh``
2
3G4Random::showEngineStatus();
4G4Random::saveEngineStatus("ファイル名");
5G4Random::restoreEngineStatus("ファイル名");

saveEnginStatusでファイルに保存できます。 デフォルトのファイル名はConfig.confになっています。

$ cat Config.conf
mixmax state, file version 1.0
N=17; V[N]={414469184642473095, 1887497230718120570, 2195475624800485765, 932596286296600524, 218172641998458221, 1120322260615769150, 539175102615237389, 1629436967027825808, 719649398285478826, 1292536263533526791, 1569870862419815394, 1958103466980645205, 824192756230114970, 1491737190745264678, 601594138938872905, 896013831305951495, 207235109353979734}; counter=17; sumtot=51334242799068912;

時刻ベースの乱数を設定したい(G4Random::setTheSeeds

 1#include <chrono>
 2#include "Randomize.hh"
 3
 4long seeds[2];
 5
 6// 現在時刻
 7auto now = std::chrono::high_resolution_clock::now();
 8// エポックからの時刻に変換
 9auto duration = now.time_since_epoch();
10
11// ナノ秒に変換してシードにする
12seeds[0] = duration.count();
13// 一様乱数と組み合わせてシードにする
14seeds[1] = duration.count() * G4UniformRand();
15
16// シードをセットする
17G4Random::setTheSeeds(seeds);

<chrono>モジュールを使って時刻ベースの乱数を設定する方法です。