Project
Loading...
Searching...
No Matches
RngHelper.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12/*
13 * RngHelper.h
14 *
15 * Created on: Jun 18, 2018
16 * Author: swenzel
17 */
18
19#ifndef COMMON_UTILS_INCLUDE_COMMONUTILS_RNGHELPER_H_
20#define COMMON_UTILS_INCLUDE_COMMONUTILS_RNGHELPER_H_
21
22#include <TRandom.h>
23#include <fcntl.h>
24
25namespace o2
26{
27namespace utils
28{
29
30// helper functions for random number (management)
32{
33 public:
34 // sets the state of the currently active ROOT gRandom Instance
35 // if 0 is given ... we will init with a random seed
36 // returns seed set to TRandom
37 static ULong_t setGRandomSeed(ULong_t seed = 0)
38 {
39 const auto s = seed == 0 ? readURandom<ULong_t>() : seed;
40 gRandom->SetSeed(s);
41 return s;
42 }
43
44 // static function to get a true random number from /dev/urandom
45 template <typename T>
46 static T readURandom()
47 {
48 int randomDataHandle = open("/dev/urandom", O_RDONLY);
49 if (randomDataHandle < 0) {
50 // something went wrong
51 } else {
52 T seed;
53 auto result = read(randomDataHandle, &seed, sizeof(T));
54 if (result < 0) {
55 // something went wrong
56 }
57 close(randomDataHandle);
58 return seed;
59 }
60 return T(0);
61 }
62};
63
64} // namespace utils
65} // namespace o2
66
67#endif /* COMMON_UTILS_INCLUDE_COMMONUTILS_RNGHELPER_H_ */
static T readURandom()
Definition RngHelper.h:46
static ULong_t setGRandomSeed(ULong_t seed=0)
Definition RngHelper.h:37
GLuint64EXT * result
Definition glcorearb.h:5662
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
Common utility functions.