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