/
quickrand.erl
385 lines (308 loc) · 11.6 KB
/
quickrand.erl
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
%-*-Mode:erlang;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*-
% ex: set ft=erlang fenc=utf-8 sts=4 ts=4 sw=4 et nomod:
%%%
%%%------------------------------------------------------------------------
%%% @doc
%%% ==Quick Random Number Generation==
%%% @end
%%%
%%% MIT License
%%%
%%% Copyright (c) 2012-2017 Michael Truog <mjtruog at gmail dot com>
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a
%%% copy of this software and associated documentation files (the "Software"),
%%% to deal in the Software without restriction, including without limitation
%%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
%%% and/or sell copies of the Software, and to permit persons to whom the
%%% Software is furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in
%%% all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
%%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
%%% DEALINGS IN THE SOFTWARE.
%%%
%%% @author Michael Truog <mjtruog [at] gmail (dot) com>
%%% @copyright 2012-2017 Michael Truog
%%% @version 1.7.2 {@date} {@time}
%%%------------------------------------------------------------------------
-module(quickrand).
-author('mjtruog [at] gmail (dot) com').
%% external interface
-export([seed/0,
uniform/1,
uniform_cache/1,
uniform_cache/2,
strong_uniform/1,
strong_uniform_range/2,
strong_float/0,
strong_floatL/0,
strong_floatM/0,
strong_floatR/0]).
-ifdef(ERLANG_OTP_VERSION_16).
-else.
-ifdef(ERLANG_OTP_VERSION_17).
-else.
-define(ERLANG_OTP_VERSION_18_FEATURES, true).
-ifdef(ERLANG_OTP_VERSION_18).
-else.
-ifdef(ERLANG_OTP_VERSION_19).
-else.
-define(ERLANG_OTP_VERSION_20_FEATURES, true).
-endif.
-endif.
-endif.
-endif.
-include("quickrand_internal.hrl").
%%%------------------------------------------------------------------------
%%% External interface functions
%%%------------------------------------------------------------------------
%%-------------------------------------------------------------------------
%% @doc
%% ===Seed random number generation.===
%% @end
%%-------------------------------------------------------------------------
-spec seed() ->
'ok'.
seed() ->
% to provide better seeding than erlang:now() or os:timestamp()
<<I1:32/unsigned-integer,
I2:32/unsigned-integer,
I3:32/unsigned-integer,
I4:32/unsigned-integer>> = crypto:strong_rand_bytes(16),
% only use positive integers for setting seed values
IP1 = I1 + 1,
IP2 = I2 + 1,
IP3 = I3 + 1,
IP4 = I4 + 1,
_ = random_wh82:seed(IP1, IP2, IP3),
_ = random_wh06_int:seed(IP1, IP2, IP3, IP4),
ok = seed_rand(IP1, IP2, IP3),
ok.
%%-------------------------------------------------------------------------
%% @doc
%% ===Quick uniform random number generation.===
%% Not meant for cryptographic purposes.
%% @end
%%-------------------------------------------------------------------------
-spec uniform(N :: pos_integer()) ->
pos_integer().
-ifdef(ERLANG_OTP_VERSION_18_FEATURES).
uniform(N) when is_integer(N), N < 1 ->
erlang:exit(badarg);
uniform(1) ->
1;
uniform(N) when is_integer(N), N < 1000000 ->
% os:timestamp/0 is currently the quickest source of uniform randomness
{_, _, MicroSecs} = os:timestamp(),
(MicroSecs rem N) + 1;
uniform(N) when is_integer(N), N =< 16#ffffffff ->
(erlang:abs(erlang:monotonic_time() bxor
erlang:unique_integer()) rem N) + 1;
uniform(N) when is_integer(N), N =< 16#03ffffffffffffff ->
% assuming exsplus/exsp for 58 bits, period 8.31e34
rand:uniform(N);
uniform(N) when is_integer(N), N =< 21267638781707063560975648195455661513 ->
% 21267638781707063560975648195455661513 ==
% 2147483579 * 2147483543 * 2147483423 * 2147483123, period 2.66e36
random_wh06_int:uniform(N);
uniform(N) when is_integer(N), N > 21267638781707063560975648195455661513 ->
strong_uniform(N).
-else.
uniform(N) when is_integer(N), N < 1 ->
erlang:exit(badarg);
uniform(1) ->
1;
uniform(N) when is_integer(N), N < 1000000 ->
% os:timestamp/0 is currently the quickest source of uniform randomness
{_, _, MicroSecs} = os:timestamp(),
(MicroSecs rem N) + 1;
uniform(N) when is_integer(N), N =< 27817185604309 ->
% 27817185604309 == 30269 * 30307 * 30323, period 2.78e13
random_wh82:uniform(N);
uniform(N) when is_integer(N), N =< 21267638781707063560975648195455661513 ->
% 21267638781707063560975648195455661513 ==
% 2147483579 * 2147483543 * 2147483423 * 2147483123, period 2.66e36
random_wh06_int:uniform(N);
uniform(N) when is_integer(N), N > 21267638781707063560975648195455661513 ->
strong_uniform(N).
-endif.
%%-------------------------------------------------------------------------
%% @doc
%% ===Quick uniform random number generation with cached data.===
%% Not meant for cryptographic purposes.
%% @end
%%-------------------------------------------------------------------------
-spec uniform_cache(N :: pos_integer()) ->
pos_integer().
-ifdef(ERLANG_OTP_VERSION_18_FEATURES).
uniform_cache(N) when is_integer(N), N < 1 ->
erlang:exit(badarg);
uniform_cache(1) ->
1;
uniform_cache(N) when is_integer(N), N < 1000000 ->
% os:timestamp/0 is currently the quickest source of uniform randomness
{_, _, MicroSecs} = os:timestamp(),
(MicroSecs rem N) + 1;
uniform_cache(N) when is_integer(N), N =< 16#ffffffff ->
(erlang:abs(erlang:monotonic_time() bxor
erlang:unique_integer()) rem N) + 1;
uniform_cache(N) when is_integer(N), N > 16#ffffffff ->
quickrand_cache:uniform(N).
-else.
uniform_cache(N) when is_integer(N), N < 1 ->
erlang:exit(badarg);
uniform_cache(1) ->
1;
uniform_cache(N) when is_integer(N), N < 1000000 ->
% os:timestamp/0 is currently the quickest source of uniform randomness
{_, _, MicroSecs} = os:timestamp(),
(MicroSecs rem N) + 1;
uniform_cache(N) when is_integer(N), N >= 1000000 ->
quickrand_cache:uniform(N).
-endif.
%%-------------------------------------------------------------------------
%% @doc
%% ===Quick uniform random number generation with cached data.===
%% Not meant for cryptographic purposes.
%% @end
%%-------------------------------------------------------------------------
-spec uniform_cache(N :: pos_integer(),
State :: quickrand_cache:state()) ->
{pos_integer(), quickrand_cache:state()}.
-ifdef(ERLANG_OTP_VERSION_18_FEATURES).
uniform_cache(N, _) when is_integer(N), N < 1 ->
erlang:exit(badarg);
uniform_cache(1, State) ->
{1, State};
uniform_cache(N, State) when is_integer(N), N < 1000000 ->
% os:timestamp/0 is currently the quickest source of uniform randomness
{_, _, MicroSecs} = os:timestamp(),
{(MicroSecs rem N) + 1, State};
uniform_cache(N, State) when is_integer(N), N =< 16#ffffffff ->
{(erlang:abs(erlang:monotonic_time() bxor
erlang:unique_integer()) rem N) + 1, State};
uniform_cache(N, State) when is_integer(N), N > 16#ffffffff ->
quickrand_cache:uniform(N, State).
-else.
uniform_cache(N, _) when is_integer(N), N < 1 ->
erlang:exit(badarg);
uniform_cache(1, State) ->
{1, State};
uniform_cache(N, State) when is_integer(N), N < 1000000 ->
% os:timestamp/0 is currently the quickest source of uniform randomness
{_, _, MicroSecs} = os:timestamp(),
{(MicroSecs rem N) + 1, State};
uniform_cache(N, State) when is_integer(N), N >= 1000000 ->
quickrand_cache:uniform(N, State).
-endif.
%%-------------------------------------------------------------------------
%% @doc
%% ===Strong uniform random number generation.===
%% @end
%%-------------------------------------------------------------------------
-spec strong_uniform(N :: pos_integer()) ->
pos_integer().
strong_uniform(N) when is_integer(N), N < 1 ->
erlang:exit(badarg);
strong_uniform(1) ->
1;
strong_uniform(N) when is_integer(N), N > 1 ->
Bytes = bytes(N),
Bits = Bytes * 8,
<<I:Bits/integer>> = crypto:strong_rand_bytes(Bytes),
(I rem N) + 1.
%%-------------------------------------------------------------------------
%% @doc
%% ===Strong uniform random number generation in a range.===
%% @end
%%-------------------------------------------------------------------------
-spec strong_uniform_range(Min :: non_neg_integer(),
Max :: non_neg_integer()) ->
non_neg_integer().
strong_uniform_range(Min, Min) ->
Min;
strong_uniform_range(Min, Max)
when is_integer(Min), is_integer(Max), Min < Max ->
strong_uniform(1 + Max - Min) - 1 + Min.
%%-------------------------------------------------------------------------
%% @doc
%% ===Return an Erlang double-precision random number with the range [0.0 .. 1.0].===
%% @end
%%-------------------------------------------------------------------------
-spec strong_float() ->
float().
strong_float() ->
% 53 bits maximum for double precision floating point representation
% (need to use a maximum value of math:pow(2, 53) with extra bit,
% i.e. 1 + 16#1fffffffffffff)
<<Bit:1, I:53/unsigned-integer, _:2>> = crypto:strong_rand_bytes(7),
if
Bit == 1, I == 0 ->
1.0;
true ->
I * ?DBL_EPSILON_DIV2
end.
%%-------------------------------------------------------------------------
%% @doc
%% ===Return an Erlang double-precision random number with the range [0.0 .. 1.0).===
%% Left portion of the 0.0 to 1.0 range.
%% @end
%%-------------------------------------------------------------------------
-spec strong_floatL() ->
float().
strong_floatL() ->
<<I:53/unsigned-integer, _:3>> = crypto:strong_rand_bytes(7),
I * ?DBL_EPSILON_DIV2.
%%-------------------------------------------------------------------------
%% @doc
%% ===Return an Erlang double-precision random number with the range (0.0 .. 1.0).===
%% Middle portion of the 0.0 to 1.0 range.
%% @end
%%-------------------------------------------------------------------------
-spec strong_floatM() ->
float().
strong_floatM() ->
<<I:53/unsigned-integer, _:3>> = crypto:strong_rand_bytes(7),
if
I == 0 ->
% almost never executes this case, an additional function call
% is necessary to have a uniform distribution
strong_floatM();
true ->
I * ?DBL_EPSILON_DIV2
end.
%%-------------------------------------------------------------------------
%% @doc
%% ===Return an Erlang double-precision random number with the range (0.0 .. 1.0].===
%% Right portion of the 0.0 to 1.0 range.
%% @end
%%-------------------------------------------------------------------------
-spec strong_floatR() ->
float().
strong_floatR() ->
<<I:53/unsigned-integer, _:3>> = crypto:strong_rand_bytes(7),
(I + 1) * ?DBL_EPSILON_DIV2.
%%%------------------------------------------------------------------------
%%% Private functions
%%%------------------------------------------------------------------------
-ifdef(ERLANG_OTP_VERSION_20_FEATURES).
seed_rand(B1, B2, B3) ->
_ = rand:seed(exsp, {B1, B2, B3}),
ok.
-else.
-ifdef(ERLANG_OTP_VERSION_18_FEATURES).
seed_rand(B1, B2, B3) ->
_ = rand:seed(exsplus, {B1, B2, B3}),
ok.
-else.
seed_rand(_, _, _) ->
ok.
-endif.
-endif.