Loading...
Searching...
No Matches
as_random.h
Go to the documentation of this file.
1/*
2 * Copyright 2008-2024 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
22extern "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 */
33typedef 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 */
46AS_EXTERN void
48
49/**
50 * Get thread local random instance.
51 */
54
55/**
56 * Get random unsigned 64 bit integer from given as_random instance
57 * using xorshift128+ algorithm: http://xorshift.di.unimi.it
58 */
59static 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 */
74static 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 */
83static 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 */
93static 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 */
102AS_EXTERN void
103as_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 */
108static inline void
109as_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 */
119AS_EXTERN void
120as_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 */
126static inline void
127as_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
AS_EXTERN as_random * as_random_instance(void)
AS_EXTERN void as_random_init(as_random *random)
static uint32_t as_random_get_uint32(void)
Definition as_random.h:94
static uint32_t as_random_next_uint32(as_random *random)
Definition as_random.h:75
static uint64_t as_random_next_uint64(as_random *random)
Definition as_random.h:60
static void as_random_get_str(char *str, uint32_t len)
Definition as_random.h:127
AS_EXTERN void as_random_next_bytes(as_random *random, uint8_t *bytes, uint32_t len)
static void as_random_get_bytes(uint8_t *bytes, uint32_t len)
Definition as_random.h:109
AS_EXTERN void as_random_next_str(as_random *random, char *str, uint32_t len)
static uint64_t as_random_get_uint64(void)
Definition as_random.h:84
#define AS_EXTERN
Definition as_std.h:25
uint64_t seed0
Definition as_random.h:34
bool initialized
Definition as_random.h:36
uint64_t seed1
Definition as_random.h:35