Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* CC0 (Public domain) - see LICENSE file for details */
#if !defined(_isaac_H)
# define _isaac_H (1)
# include <stdint.h>
typedef struct isaac_ctx isaac_ctx;
/*This value may be lowered to reduce memory usage on embedded platforms, at
the cost of reducing security and increasing bias.
Quoting Bob Jenkins: "The current best guess is that bias is detectable after
2**37 values for [ISAAC_SZ_LOG]=3, 2**45 for 4, 2**53 for 5, 2**61 for 6,
2**69 for 7, and 2**77 values for [ISAAC_SZ_LOG]=8."*/
#define ISAAC_SZ_LOG (8)
#define ISAAC_SZ (1<<ISAAC_SZ_LOG)
#define ISAAC_SEED_SZ_MAX (ISAAC_SZ<<2)
/*ISAAC is the most advanced of a series of pseudo-random number generators
designed by Robert J. Jenkins Jr. in 1996.
http://www.burtleburtle.net/bob/rand/isaac.html
To quote:
No efficient method is known for deducing their internal states.
ISAAC requires an amortized 18.75 instructions to produce a 32-bit value.
There are no cycles in ISAAC shorter than 2**40 values.
The expected cycle length is 2**8295 values.*/
struct isaac_ctx{
unsigned n;
uint32_t r[ISAAC_SZ];
uint32_t m[ISAAC_SZ];
uint32_t a;
uint32_t b;
uint32_t c;
};
/**
* isaac_init - Initialize an instance of the ISAAC random number generator.
* @_ctx: The instance to initialize.
* @_seed: The specified seed bytes.
* This may be NULL if _nseed is less than or equal to zero.
* @_nseed: The number of bytes to use for the seed.
* If this is greater than ISAAC_SEED_SZ_MAX, the extra bytes are
* ignored.
*/
void isaac_init(isaac_ctx *_ctx,const unsigned char *_seed,int _nseed);
/**
* isaac_reseed - Mix a new batch of entropy into the current state.
* To reset ISAAC to a known state, call isaac_init() again instead.
* @_ctx: The instance to reseed.
* @_seed: The specified seed bytes.
* This may be NULL if _nseed is zero.
* @_nseed: The number of bytes to use for the seed.
* If this is greater than ISAAC_SEED_SZ_MAX, the extra bytes are
* ignored.
*/
void isaac_reseed(isaac_ctx *_ctx,const unsigned char *_seed,int _nseed);
/**
* isaac_next_uint32 - Return the next random 32-bit value.
* @_ctx: The ISAAC instance to generate the value with.
*/
uint32_t isaac_next_uint32(isaac_ctx *_ctx);
/**
* isaac_next_uint - Uniform random integer less than the given value.
* @_ctx: The ISAAC instance to generate the value with.
* @_n: The upper bound on the range of numbers returned (not inclusive).
* This must be greater than zero and less than 2**32.
* To return integers in the full range 0...2**32-1, use
* isaac_next_uint32() instead.
* Return: An integer uniformly distributed between 0 and _n-1 (inclusive).
*/
uint32_t isaac_next_uint(isaac_ctx *_ctx,uint32_t _n);
/**
* isaac_next_float - Uniform random float in the range [0,1).
* @_ctx: The ISAAC instance to generate the value with.
* Returns a high-quality float uniformly distributed between 0 (inclusive)
* and 1 (exclusive).
* All of the float's mantissa bits are random, e.g., the least significant bit
* may still be non-zero even if the value is less than 0.5, and any
* representable float in the range [0,1) has a chance to be returned, though
* values very close to zero become increasingly unlikely.
* To generate cheaper float values that do not have these properties, use
* ldexpf((float)isaac_next_uint32(_ctx),-32);
*/
float isaac_next_float(isaac_ctx *_ctx);
/**
* isaac_next_signed_float - Uniform random float in the range (-1,1).
* @_ctx: The ISAAC instance to generate the value with.
* Returns a high-quality float uniformly distributed between -1 and 1
* (exclusive).
* All of the float's mantissa bits are random, e.g., the least significant bit
* may still be non-zero even if the magnitude is less than 0.5, and any
* representable float in the range (-1,1) has a chance to be returned, though
* values very close to zero become increasingly unlikely.
* To generate cheaper float values that do not have these properties, use
* ldexpf((float)isaac_next_uint32(_ctx),-31)-1;
* though this returns values in the range [-1,1).
*/
float isaac_next_signed_float(isaac_ctx *_ctx);
/**
* isaac_next_double - Uniform random double in the range [0,1).
* @_ctx: The ISAAC instance to generate the value with.
* Returns a high-quality double uniformly distributed between 0 (inclusive)
* and 1 (exclusive).
* All of the double's mantissa bits are random, e.g., the least significant
* bit may still be non-zero even if the value is less than 0.5, and any
* representable double in the range [0,1) has a chance to be returned, though
* values very close to zero become increasingly unlikely.
* To generate cheaper double values that do not have these properties, use
* ldexp((double)isaac_next_uint32(_ctx),-32);
*/
double isaac_next_double(isaac_ctx *_ctx);
/**
* isaac_next_signed_double - Uniform random double in the range (-1,1).
* @_ctx: The ISAAC instance to generate the value with.
* Returns a high-quality double uniformly distributed between -1 and 1
* (exclusive).
* All of the double's mantissa bits are random, e.g., the least significant
* bit may still be non-zero even if the value is less than 0.5, and any
* representable double in the range (-1,1) has a chance to be returned,
* though values very close to zero become increasingly unlikely.
* To generate cheaper double values that do not have these properties, use
* ldexp((double)isaac_next_uint32(_ctx),-31)-1;
* though this returns values in the range [-1,1).
*/
double isaac_next_signed_double(isaac_ctx *_ctx);
#endif