]> git.ozlabs.org Git - ccan/blob - ccan/isaac/isaac64.h
Relicense all public domain modules to CC0.
[ccan] / ccan / isaac / isaac64.h
1 /* CC0 (Public domain) - see LICENSE file for details */
2 #if !defined(_isaac64_H)
3 # define _isaac64_H (1)
4 # include <stdint.h>
5
6
7
8 typedef struct isaac64_ctx isaac64_ctx;
9
10
11
12 #define ISAAC64_SZ_LOG      (8)
13 #define ISAAC64_SZ          (1<<ISAAC64_SZ_LOG)
14 #define ISAAC64_SEED_SZ_MAX (ISAAC64_SZ<<3)
15
16
17
18 /*ISAAC is the most advanced of a series of pseudo-random number generators
19    designed by Robert J. Jenkins Jr. in 1996.
20   http://www.burtleburtle.net/bob/rand/isaac.html
21   This is the 64-bit version.
22   To quote:
23     ISAAC-64 generates a different sequence than ISAAC, but it uses the same
24      principles.
25     It uses 64-bit arithmetic.
26     It generates a 64-bit result every 19 instructions.
27     All cycles are at least 2**72 values, and the average cycle length is
28      2**16583.*/
29 struct isaac64_ctx{
30   unsigned n;
31   uint64_t r[ISAAC64_SZ];
32   uint64_t m[ISAAC64_SZ];
33   uint64_t a;
34   uint64_t b;
35   uint64_t c;
36 };
37
38
39 /**
40  * isaac64_init - Initialize an instance of the ISAAC64 random number generator.
41  * @_ctx:   The ISAAC64 instance to initialize.
42  * @_seed:  The specified seed bytes.
43  *          This may be NULL if _nseed is less than or equal to zero.
44  * @_nseed: The number of bytes to use for the seed.
45  *          If this is greater than ISAAC64_SEED_SZ_MAX, the extra bytes are
46  *           ignored.
47  */
48 void isaac64_init(isaac64_ctx *_ctx,const unsigned char *_seed,int _nseed);
49
50 /**
51  * isaac64_reseed - Mix a new batch of entropy into the current state.
52  * To reset ISAAC64 to a known state, call isaac64_init() again instead.
53  * @_ctx:   The instance to reseed.
54  * @_seed:  The specified seed bytes.
55  *          This may be NULL if _nseed is zero.
56  * @_nseed: The number of bytes to use for the seed.
57  *          If this is greater than ISAAC64_SEED_SZ_MAX, the extra bytes are
58  *           ignored.
59  */
60 void isaac64_reseed(isaac64_ctx *_ctx,const unsigned char *_seed,int _nseed);
61 /**
62  * isaac64_next_uint64 - Return the next random 64-bit value.
63  * @_ctx: The ISAAC64 instance to generate the value with.
64  */
65 uint64_t isaac64_next_uint64(isaac64_ctx *_ctx);
66 /**
67  * isaac64_next_uint - Uniform random integer less than the given value.
68  * @_ctx: The ISAAC64 instance to generate the value with.
69  * @_n:   The upper bound on the range of numbers returned (not inclusive).
70  *        This must be greater than zero and less than 2**64.
71  *        To return integers in the full range 0...2**64-1, use
72  *         isaac64_next_uint64() instead.
73  * Return: An integer uniformly distributed between 0 and _n-1 (inclusive).
74  */
75 uint64_t isaac64_next_uint(isaac64_ctx *_ctx,uint64_t _n);
76 /**
77  * isaac64_next_float - Uniform random float in the range [0,1).
78  * @_ctx: The ISAAC64 instance to generate the value with.
79  * Returns a high-quality float uniformly distributed between 0 (inclusive)
80  *  and 1 (exclusive).
81  * All of the float's mantissa bits are random, e.g., the least significant bit
82  *  may still be non-zero even if the value is less than 0.5, and any
83  *  representable float in the range [0,1) has a chance to be returned, though
84  *  values very close to zero become increasingly unlikely.
85  * To generate cheaper float values that do not have these properties, use
86  *   ldexpf((float)isaac64_next_uint64(_ctx),-64);
87  */
88 float isaac64_next_float(isaac64_ctx *_ctx);
89 /**
90  * isaac64_next_signed_float - Uniform random float in the range (-1,1).
91  * @_ctx: The ISAAC64 instance to generate the value with.
92  * Returns a high-quality float uniformly distributed between -1 and 1
93  *  (exclusive).
94  * All of the float's mantissa bits are random, e.g., the least significant bit
95  *  may still be non-zero even if the magnitude is less than 0.5, and any
96  *  representable float in the range (-1,1) has a chance to be returned, though
97  *  values very close to zero become increasingly unlikely.
98  * To generate cheaper float values that do not have these properties, use
99  *   ldexpf((float)isaac64_next_uint64(_ctx),-63)-1;
100  *  though this returns values in the range [-1,1).
101  */
102 float isaac64_next_signed_float(isaac64_ctx *_ctx);
103 /**
104  * isaac64_next_double - Uniform random double in the range [0,1).
105  * @_ctx: The ISAAC64 instance to generate the value with.
106  * Returns a high-quality double uniformly distributed between 0 (inclusive)
107  *  and 1 (exclusive).
108  * All of the double's mantissa bits are random, e.g., the least significant
109  *  bit may still be non-zero even if the value is less than 0.5, and any
110  *  representable double in the range [0,1) has a chance to be returned, though
111  *  values very close to zero become increasingly unlikely.
112  * To generate cheaper double values that do not have these properties, use
113  *   ldexp((double)isaac64_next_uint64(_ctx),-64);
114  */
115 double isaac64_next_double(isaac64_ctx *_ctx);
116 /**
117  * isaac64_next_signed_double - Uniform random double in the range (-1,1).
118  * @_ctx: The ISAAC64 instance to generate the value with.
119  * Returns a high-quality double uniformly distributed between -1 and 1
120  *  (exclusive).
121  * All of the double's mantissa bits are random, e.g., the least significant
122  *  bit may still be non-zero even if the value is less than 0.5, and any
123  *  representable double in the range (-1,1) has a chance to be returned,
124  *  though values very close to zero become increasingly unlikely.
125  * To generate cheaper double values that do not have these properties, use
126  *   ldexp((double)isaac64_next_uint64(_ctx),-63)-1;
127  *  though this returns values in the range [-1,1).
128  */
129 double isaac64_next_signed_double(isaac64_ctx *_ctx);
130
131 #endif