All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_random.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2020 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 #pragma once
18 
19 #include <aerospike/as_std.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /******************************************************************************
26  * Types
27  *****************************************************************************/
28 
29 /**
30  * Random seeds used in xorshift128+ algorithm: http://xorshift.di.unimi.it
31  * Not thread-safe. Instantiate once per thread.
32  */
33 typedef struct as_random_s {
34  uint64_t seed0;
35  uint64_t seed1;
37 } as_random;
38 
39 /*******************************************************************************
40  * Functions
41  ******************************************************************************/
42 
43 /**
44  * Initialize random instance.
45  */
46 AS_EXTERN void
47 as_random_init(as_random* random);
48 
49 /**
50  * Get thread local random instance.
51  */
53 as_random_instance(void);
54 
55 /**
56  * Get random unsigned 64 bit integer from given as_random instance
57  * using xorshift128+ algorithm: http://xorshift.di.unimi.it
58  */
59 static inline uint64_t
61 {
62  // Use xorshift128+ algorithm.
63  uint64_t s1 = random->seed0;
64  const uint64_t s0 = random->seed1;
65  random->seed0 = s0;
66  s1 ^= s1 << 23;
67  random->seed1 = (s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5));
68  return random->seed1 + s0;
69 }
70 
71 /**
72  * Get random unsigned 32 bit integer from given as_random instance.
73  */
74 static inline uint32_t
76 {
77  return (uint32_t)as_random_next_uint64(random);
78 }
79 
80 /**
81  * Get random unsigned 64 bit integer from thread local instance.
82  */
83 static inline uint64_t
85 {
86  as_random* random = as_random_instance();
87  return as_random_next_uint64(random);
88 }
89 
90 /**
91  * Get random unsigned 32 bit integer from thread local instance.
92  */
93 static inline uint32_t
95 {
96  return (uint32_t)as_random_get_uint64();
97 }
98 
99 /**
100  * Get random bytes of specified length from given as_random instance.
101  */
102 AS_EXTERN void
103 as_random_next_bytes(as_random* random, uint8_t* bytes, uint32_t len);
104 
105 /**
106  * Get random bytes of specified length from thread local instance.
107  */
108 static inline void
109 as_random_get_bytes(uint8_t* bytes, uint32_t len)
110 {
111  as_random* random = as_random_instance();
112  as_random_next_bytes(random, bytes, len);
113 }
114 
115 /**
116  * Get null terminated random alphanumeric string of specified length using given
117  * as_random instance. String buffer must include space for extra null byte.
118  */
119 AS_EXTERN void
120 as_random_next_str(as_random* random, char* str, uint32_t len);
121 
122 /**
123  * Get null terminated random alphanumeric string of specified length from thread
124  * local random instance. String buffer must include space for extra null byte.
125  */
126 static inline void
127 as_random_get_str(char* str, uint32_t len)
128 {
129  as_random* random = as_random_instance();
130  as_random_next_str(random, str, len);
131 }
132 
133 #ifdef __cplusplus
134 } // end extern "C"
135 #endif
static uint32_t as_random_get_uint32()
Definition: as_random.h:94
static void as_random_get_str(char *str, uint32_t len)
Definition: as_random.h:127
uint64_t seed1
Definition: as_random.h:35
AS_EXTERN void as_random_next_str(as_random *random, char *str, uint32_t len)
static uint32_t as_random_next_uint32(as_random *random)
Definition: as_random.h:75
#define AS_EXTERN
Definition: as_std.h:25
static uint64_t as_random_next_uint64(as_random *random)
Definition: as_random.h:60
AS_EXTERN void as_random_init(as_random *random)
AS_EXTERN void as_random_next_bytes(as_random *random, uint8_t *bytes, uint32_t len)
AS_EXTERN as_random * as_random_instance(void)
bool initialized
Definition: as_random.h:36
static uint64_t as_random_get_uint64()
Definition: as_random.h:84
uint64_t seed0
Definition: as_random.h:34
static void as_random_get_bytes(uint8_t *bytes, uint32_t len)
Definition: as_random.h:109