Mstdlib-1.24.0
m_rand.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2015 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_RAND_H__
25#define __M_RAND_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28
29#include <mstdlib/base/m_defs.h>
30#include <mstdlib/base/m_types.h>
31
32/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
33
34__BEGIN_DECLS
35
36/*! \addtogroup m_rand Pseudo Random Number Generator (PRNG)
37 * \ingroup mstdlib_base
38 *
39 * This is _NOT_ a cryptographically secure RNG. This should _NEVER_ be
40 * used for cryptographic operations.
41 *
42 * @{
43 */
44
45#define M_RAND_MAX M_UINT64_MAX
46
47struct M_rand;
48typedef struct M_rand M_rand_t;
49
50/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
51
52/*! Create a random state for use with random number generation.
53 *
54 * \param[in] seed The seed state. Using the same seed will allow the sequence to be repeated.
55 * If 0 the seed will be a combination of system time, local and heap memory
56 * addresses. The data is meant to make choosing a random seed harder but still
57 * is not cryptographically secure. This not being a cryptographically secure
58 * random number generator we are using random data that is not cryptographically
59 * secure for speed.
60 *
61 * \return Random state.
62 */
63M_API M_rand_t *M_rand_create(M_uint64 seed);
64
65
66/*! Destroy a random state.
67 *
68 * \param[in,out] state The state.
69 */
70M_API void M_rand_destroy(M_rand_t *state);
71
72
73/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
74
75/*! Generate a random number.
76 *
77 * Generates a random number from 0 to M_RAND_MAX.
78 *
79 * \param[in,out] state The state. Optional, can be NULL, but will incur the overhead of
80 * M_rand_create(0); M_rand_destroy(); per iteration if not provided.
81 * It is possible, in a tight loop on a very fast system for example,
82 * the same number could be generated multiple times if the state is NULL.
83 *
84 * \return A random number.
85 */
86M_API M_uint64 M_rand(M_rand_t *state);
87
88
89/*! Generate a random number within a given range.
90 *
91 * Range is [min, max). Meaning from min to max-1.
92 *
93 * \param[in,out] state The state. Optional, can be NULL, but will incur the overhead of
94 * M_rand_create(0); M_rand_destroy(); per iteration if not provided.
95 * It is possible, in a tight loop on a very fast system for example,
96 * the same number could be generated multiple times if the state is NULL.
97 * \param[in] min The min.
98 * \param[in] max The max.
99 */
100M_API M_uint64 M_rand_range(M_rand_t *state, M_uint64 min, M_uint64 max);
101
102
103/*! Generate a random number with a given maximum.
104 *
105 * Range is [0, max). Meaning from 0 to max-1.
106 *
107 * \param[in,out] state The state. Optional, can be NULL, but will incur the overhead of
108 * M_rand_create(0); M_rand_destroy(); per iteration if not provided.
109 * It is possible, in a tight loop on a very fast system for example,
110 * the same number could be generated multiple times if the state is NULL.
111 * \param[in] max The max.
112 */
113M_API M_uint64 M_rand_max(M_rand_t *state, M_uint64 max);
114
115/*! Generate a random string based on the provided character set.
116 *
117 * \param[in,out] state The state. Optional, can be NULL, but will incur the overhead of
118 * M_rand_create(0); M_rand_destroy(); per iteration if not provided.
119 * It is possible, in a tight loop on a very fast system for example,
120 * the same string could be generated multiple times if the state is NULL.
121 * \param[in] charset Character set to use to generate the random string.
122 * \param[out] out Buffer to use to hold the resulting random string. Must be
123 * len+1 bytes in length or greater to handle NULL terminator.
124 * \param[in] len Number of characters to generate. Actual number of bytes written
125 * will be 1 larger for the NULL terminator.
126 * \return M_FALSE on usage error. M_TRUE on success.
127 */
128M_API M_bool M_rand_str(M_rand_t *state, const char *charset, char *out, size_t len);
129
130
131/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
132
133/*! Duplicate the state of a random number generator.
134 *
135 * If state is NULL this is equivalent to M_rand_create(0).
136 *
137 * \param[in,out] state The state.
138 *
139 * \return Random state.
140 *
141 * \see M_rand_jump
142 */
143M_API M_rand_t *M_rand_duplicate(const M_rand_t *state);
144
145
146/*! Advance the random number generator to generate non-overlapping sequences.
147 *
148 * \param[in,out] state The state.
149 *
150 * \see M_rand_duplicate
151 */
152M_API void M_rand_jump(M_rand_t *state);
153
154/*! @} */
155
156__END_DECLS
157
158#endif /* __M_RAND_H__ */
void M_rand_destroy(M_rand_t *state)
M_rand_t * M_rand_create(M_uint64 seed)
M_uint64 M_rand(M_rand_t *state)
void M_rand_jump(M_rand_t *state)
struct M_rand M_rand_t
Definition: m_rand.h:48
M_bool M_rand_str(M_rand_t *state, const char *charset, char *out, size_t len)
M_uint64 M_rand_max(M_rand_t *state, M_uint64 max)
M_uint64 M_rand_range(M_rand_t *state, M_uint64 min, M_uint64 max)
M_rand_t * M_rand_duplicate(const M_rand_t *state)