IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
random.h
Go to the documentation of this file.
1/**
2 * \file IMP/random.h \brief Random number generators used by IMP.
3 *
4 * Copyright 2007-2026 IMP Inventors. All rights reserved.
5 *
6 */
7
8#ifndef IMPKERNEL_RANDOM_H
9#define IMPKERNEL_RANDOM_H
10
11#include <IMP/kernel_config.h>
12#include <IMP/Vector.h>
13#include <random>
14#include <sstream>
15#include <cereal/access.hpp>
16
17IMPKERNEL_BEGIN_NAMESPACE
18
19#ifndef SWIG // the RNG is defined explicitly in pyext/IMP_kernel.random.i
20
21class RandomNumberGenerator : public std::mt19937 {
22 typedef std::mt19937 T;
23 T::result_type last_seed_;
24 unsigned seed_counter_;
25
26 friend class cereal::access;
27 template<class Archive> void serialize(Archive &ar) {
28 ar(last_seed_, seed_counter_);
29
30 // Serialize the generator itself. The only supported way to get/set the
31 // internal state is via the << and >> operators (as text)
32 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
33 std::string rstate;
34 ar(rstate);
35 std::istringstream iss(rstate);
36 iss >> *this;
37 } else {
38 std::ostringstream oss;
39 oss << *this;
40 ar(oss.str());
41 }
42 }
43
44public:
45 RandomNumberGenerator()
46 : T(default_seed), last_seed_(default_seed), seed_counter_(1) {}
47
48 RandomNumberGenerator(T::result_type seed)
49 : T(seed), last_seed_(seed), seed_counter_(1) {}
50
51 void seed() { seed(default_seed); }
52
53 // Set a new seed
54 void seed(T::result_type seed) {
55 last_seed_ = seed;
56 seed_counter_++;
57 if (seed_counter_ == 0) {
58 seed_counter_ = 1;
59 }
60 T::seed(seed);
61 }
62
63 // Get the last-set seed, either from when the class was constructed or
64 // from a previous call to seed()
65 T::result_type get_last_seed() const { return last_seed_; }
66
67 // Get the counter value from when the seed was last set. This value
68 // will never be zero. This can be used to determine if the seed was
69 // changed since the last use.
70 unsigned get_seed_counter() const { return seed_counter_; }
71};
72
73//! A shared non-GPU random number generator
74/** The random number generator is seeded based on command line specified flag.
75 Otherwise, the default seed is retrieved from among either
76 boost::random_device, `/dev/urandom`, or the system clock, based on which
77 method is available in this priority order.
78
79 To set the seed, call the `seed` method, which takes a single integer
80 argument. This object is also callable, and returns a new random integer
81 on each call.
82
83This generator can be used by the
84[Boost.Random](https://www.boost.org/doc/libs/1_80_0/doc/html/boost_random.html)
85distributions.
86 */
87extern IMPKERNELEXPORT RandomNumberGenerator random_number_generator;
88#endif
89
90//! Return the initial random seed.
91/** To set the seed or get random values, see IMP::random_number_generator.
92 */
93IMPKERNELEXPORT boost::uint64_t get_random_seed();
94
95
96IMPKERNEL_END_NAMESPACE
97
98#endif /* IMPKERNEL_RANDOM_H */
A class for storing lists of IMP items.
boost::uint64_t get_random_seed()
Return the initial random seed.
RandomNumberGenerator random_number_generator
A shared non-GPU random number generator.