17 |
18 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2012-2023 Michael Truog
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a
6 | copy of this software and associated documentation files (the "Software"),
7 | to deal in the Software without restriction, including without limitation
8 | the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | and/or sell copies of the Software, and to permit persons to whom the
10 | Software is furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 | DEALINGS IN THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/mix.exs:
--------------------------------------------------------------------------------
1 | #-*-Mode:elixir;coding:utf-8;tab-width:2;c-basic-offset:2;indent-tabs-mode:()-*-
2 | # ex: set ft=elixir fenc=utf-8 sts=2 ts=2 sw=2 et nomod:
3 |
4 | defmodule Quickrand.Mixfile do
5 | use Mix.Project
6 |
7 | def project do
8 | [app: :quickrand,
9 | version: "2.0.7",
10 | language: :erlang,
11 | erlc_options: [
12 | {:d, :erlang.list_to_atom(~c"ERLANG_OTP_VERSION_" ++ :erlang.system_info(:otp_release))},
13 | :deterministic,
14 | :debug_info,
15 | :warn_export_vars,
16 | :warn_unused_import,
17 | #:warn_missing_spec,
18 | :warnings_as_errors],
19 | description: description(),
20 | package: package(),
21 | deps: deps()]
22 | end
23 |
24 | def application do
25 | [applications: [
26 | :crypto]]
27 | end
28 |
29 | defp deps do
30 | []
31 | end
32 |
33 | defp description do
34 | "Quick Random Number Generation: " <>
35 | "Provides a simple interface to call efficient random number generation " <>
36 | "functions based on the context. Proper random number seeding is enforced."
37 | end
38 |
39 | defp package do
40 | [files: ~w(src doc rebar.config README.markdown LICENSE),
41 | maintainers: ["Michael Truog"],
42 | licenses: ["MIT"],
43 | links: %{"GitHub" => "https://github.com/okeuday/quickrand"}]
44 | end
45 | end
46 |
--------------------------------------------------------------------------------
/src/quickrand_math.hrl:
--------------------------------------------------------------------------------
1 | %-*-Mode:erlang;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*-
2 | % ex: set ft=erlang fenc=utf-8 sts=4 ts=4 sw=4 et nomod:
3 | %%%
4 | %%%------------------------------------------------------------------------
5 | %%%
6 | %%% MIT License
7 | %%%
8 | %%% Copyright (c) 2017 Michael Truog
9 | %%%
10 | %%% Permission is hereby granted, free of charge, to any person obtaining a
11 | %%% copy of this software and associated documentation files (the "Software"),
12 | %%% to deal in the Software without restriction, including without limitation
13 | %%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 | %%% and/or sell copies of the Software, and to permit persons to whom the
15 | %%% Software is furnished to do so, subject to the following conditions:
16 | %%%
17 | %%% The above copyright notice and this permission notice shall be included in
18 | %%% all copies or substantial portions of the Software.
19 | %%%
20 | %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 | %%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 | %%% DEALINGS IN THE SOFTWARE.
27 | %%%
28 | %%%------------------------------------------------------------------------
29 |
30 | -define(PI2, math:pi() * 2.0).
31 |
32 |
--------------------------------------------------------------------------------
/src/quickrand_internal.hrl:
--------------------------------------------------------------------------------
1 | %-*-Mode:erlang;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*-
2 | % ex: set ft=erlang fenc=utf-8 sts=4 ts=4 sw=4 et nomod:
3 | %%%
4 | %%%------------------------------------------------------------------------
5 | %%%
6 | %%% MIT License
7 | %%%
8 | %%% Copyright (c) 2017-2022 Michael Truog
9 | %%%
10 | %%% Permission is hereby granted, free of charge, to any person obtaining a
11 | %%% copy of this software and associated documentation files (the "Software"),
12 | %%% to deal in the Software without restriction, including without limitation
13 | %%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 | %%% and/or sell copies of the Software, and to permit persons to whom the
15 | %%% Software is furnished to do so, subject to the following conditions:
16 | %%%
17 | %%% The above copyright notice and this permission notice shall be included in
18 | %%% all copies or substantial portions of the Software.
19 | %%%
20 | %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 | %%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 | %%% DEALINGS IN THE SOFTWARE.
27 | %%%
28 | %%%------------------------------------------------------------------------
29 |
30 | -define(BYTES_RESOLUTION, 4). % bytes
31 |
32 | bytes(I) when is_integer(I), I > 0 ->
33 | bytes(I, 0).
34 |
35 | bytes(0, Bytes) ->
36 | Bytes;
37 | bytes(I, Bytes) ->
38 | bytes(I bsr (?BYTES_RESOLUTION * 8), Bytes + ?BYTES_RESOLUTION).
39 |
40 |
--------------------------------------------------------------------------------
/src/quickrand_test.hrl:
--------------------------------------------------------------------------------
1 | %-*-Mode:erlang;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*-
2 | % ex: set ft=erlang fenc=utf-8 sts=4 ts=4 sw=4 et nomod:
3 | %%%
4 | %%%------------------------------------------------------------------------
5 | %%% quickrand eunit common functionality
6 | %%%
7 | %%% MIT License
8 | %%%
9 | %%% Copyright (c) 2020 Michael Truog
10 | %%%
11 | %%% Permission is hereby granted, free of charge, to any person obtaining a
12 | %%% copy of this software and associated documentation files (the "Software"),
13 | %%% to deal in the Software without restriction, including without limitation
14 | %%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 | %%% and/or sell copies of the Software, and to permit persons to whom the
16 | %%% Software is furnished to do so, subject to the following conditions:
17 | %%%
18 | %%% The above copyright notice and this permission notice shall be included in
19 | %%% all copies or substantial portions of the Software.
20 | %%%
21 | %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 | %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 | %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 | %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 | %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 | %%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 | %%% DEALINGS IN THE SOFTWARE.
28 | %%%
29 | %%%------------------------------------------------------------------------
30 |
31 | -ifndef(_assertOk).
32 | -define(_assertOk(Expr), ?_assertEqual(ok, Expr)).
33 | -endif.
34 |
35 | -ifdef(CLOUDI_TEST_TIMEOUT).
36 | -define(TEST_TIMEOUT, ?CLOUDI_TEST_TIMEOUT). % seconds
37 | -else.
38 | -define(TEST_TIMEOUT, 10). % seconds
39 | -endif.
40 |
41 |
--------------------------------------------------------------------------------
/src/quickrand_constants.hrl:
--------------------------------------------------------------------------------
1 | %-*-Mode:erlang;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*-
2 | % ex: set ft=erlang fenc=utf-8 sts=4 ts=4 sw=4 et nomod:
3 | %%%
4 | %%%------------------------------------------------------------------------
5 | %%%
6 | %%% MIT License
7 | %%%
8 | %%% Copyright (c) 2022 Michael Truog
9 | %%%
10 | %%% Permission is hereby granted, free of charge, to any person obtaining a
11 | %%% copy of this software and associated documentation files (the "Software"),
12 | %%% to deal in the Software without restriction, including without limitation
13 | %%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 | %%% and/or sell copies of the Software, and to permit persons to whom the
15 | %%% Software is furnished to do so, subject to the following conditions:
16 | %%%
17 | %%% The above copyright notice and this permission notice shall be included in
18 | %%% all copies or substantial portions of the Software.
19 | %%%
20 | %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 | %%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 | %%% DEALINGS IN THE SOFTWARE.
27 | %%%
28 | %%%------------------------------------------------------------------------
29 |
30 | -define(APPLICATION, quickrand).
31 |
32 | % 1 / (1 + 16#1fffffffffffff) =:= math:pow(2, -53) to provide [0.0 .. 1.0]
33 | -define(DBL_EPSILON_DIV2, 1.1102230246251565e-16).
34 |
35 | -define(BITMASK_16, 16#ffff).
36 | -define(BITMASK_32, 16#ffffffff).
37 | -define(BITMASK_35, 16#7ffffffff).
38 | -define(BITMASK_59, 16#7ffffffffffffff).
39 | -define(BITMASK_64, 16#ffffffffffffffff).
40 | -define(BITMASK_128, 16#ffffffffffffffffffffffffffffffff).
41 | -define(BITMASK_1024, 16#ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff).
42 |
43 |
--------------------------------------------------------------------------------
/doc/quickrand_normal.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Module quickrand_normal
6 |
7 |
8 |
9 |
38 | Has numerical stability problems when X1 is very close to zero.
39 | This is a serious problem for stochastic modeling that is generating
40 | millions of numbers. If the result is used with added noise
41 | (e.g., creating a process sleep value) then it isn't a problem.
42 |
43 |
44 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/src/quickrand_normal.erl:
--------------------------------------------------------------------------------
1 | %-*-Mode:erlang;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*-
2 | % ex: set ft=erlang fenc=utf-8 sts=4 ts=4 sw=4 et nomod:
3 | %%%
4 | %%%------------------------------------------------------------------------
5 | %%% @doc
6 | %%% ==Quick Normal Distribution Random Number Generation==
7 | %%% @end
8 | %%%
9 | %%% MIT License
10 | %%%
11 | %%% Copyright (c) 2017-2022 Michael Truog
12 | %%%
13 | %%% Permission is hereby granted, free of charge, to any person obtaining a
14 | %%% copy of this software and associated documentation files (the "Software"),
15 | %%% to deal in the Software without restriction, including without limitation
16 | %%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 | %%% and/or sell copies of the Software, and to permit persons to whom the
18 | %%% Software is furnished to do so, subject to the following conditions:
19 | %%%
20 | %%% The above copyright notice and this permission notice shall be included in
21 | %%% all copies or substantial portions of the Software.
22 | %%%
23 | %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 | %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 | %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 | %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 | %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 | %%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 | %%% DEALINGS IN THE SOFTWARE.
30 | %%%
31 | %%% @author Michael Truog
32 | %%% @copyright 2017-2022 Michael Truog
33 | %%% @version 2.0.5 {@date} {@time}
34 | %%%------------------------------------------------------------------------
35 |
36 | -module(quickrand_normal).
37 | -author('mjtruog at protonmail dot com').
38 |
39 | %% external interface
40 | -export([box_muller/2]).
41 |
42 | -include("quickrand_math.hrl").
43 |
44 | %%%------------------------------------------------------------------------
45 | %%% External interface functions
46 | %%%------------------------------------------------------------------------
47 |
48 | %%-------------------------------------------------------------------------
49 | %% @doc
50 | %% ===Box-Muller transformation for generating Gaussian noise.===
51 | %% Has numerical stability problems when X1 is very close to zero.
52 | %% This is a serious problem for stochastic modeling that is generating
53 | %% millions of numbers. If the result is used with added noise
54 | %% (e.g., creating a process sleep value) then it isn't a problem.
55 | %% @end
56 | %%-------------------------------------------------------------------------
57 |
58 | -spec box_muller(Mean :: number(),
59 | StdDev :: number()) ->
60 | {Result1 :: float(), Result2 :: float()}.
61 |
62 | box_muller(Mean, StdDev) ->
63 | % use Box-Muller transformation to generate Gaussian noise
64 | %
65 | % George Edward Pelham Box, Mervin Edgar Muller.
66 | % A Note on the Generation of Random Normal Deviates.
67 | % The Annals of Mathematical Statistics,
68 | % vol. 29, no. 2, pp. 610–611, 1958.
69 | X1 = quickrand:strong_floatR(),
70 | X2 = ?PI2 * quickrand:strong_floatR(),
71 | K = StdDev * math:sqrt(-2.0 * math:log(X1)),
72 | Result1 = Mean + K * math:cos(X2),
73 | Result2 = Mean + K * math:sin(X2),
74 | {Result1, Result2}.
75 |
76 | %%%------------------------------------------------------------------------
77 | %%% Private functions
78 | %%%------------------------------------------------------------------------
79 |
80 |
--------------------------------------------------------------------------------
/src/quickrand_cache_normal.erl:
--------------------------------------------------------------------------------
1 | %-*-Mode:erlang;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*-
2 | % ex: set ft=erlang fenc=utf-8 sts=4 ts=4 sw=4 et nomod:
3 | %%%
4 | %%%------------------------------------------------------------------------
5 | %%% @doc
6 | %%% ==Quick Normal Distribution Random Number Generation With Cached Data==
7 | %%% @end
8 | %%%
9 | %%% MIT License
10 | %%%
11 | %%% Copyright (c) 2017 Michael Truog
12 | %%%
13 | %%% Permission is hereby granted, free of charge, to any person obtaining a
14 | %%% copy of this software and associated documentation files (the "Software"),
15 | %%% to deal in the Software without restriction, including without limitation
16 | %%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 | %%% and/or sell copies of the Software, and to permit persons to whom the
18 | %%% Software is furnished to do so, subject to the following conditions:
19 | %%%
20 | %%% The above copyright notice and this permission notice shall be included in
21 | %%% all copies or substantial portions of the Software.
22 | %%%
23 | %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 | %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 | %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 | %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 | %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 | %%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 | %%% DEALINGS IN THE SOFTWARE.
30 | %%%
31 | %%% @author Michael Truog
32 | %%% @copyright 2017 Michael Truog
33 | %%% @version 1.7.2 {@date} {@time}
34 | %%%------------------------------------------------------------------------
35 |
36 | -module(quickrand_cache_normal).
37 | -author('mjtruog at protonmail dot com').
38 |
39 | %% external interface
40 | -export([box_muller/2,
41 | box_muller/3]).
42 |
43 | -include("quickrand_math.hrl").
44 |
45 | %%%------------------------------------------------------------------------
46 | %%% External interface functions
47 | %%%------------------------------------------------------------------------
48 |
49 | %%-------------------------------------------------------------------------
50 | %% @doc
51 | %% ===Process dictionary cache version of quickrand_normal:box_muller/2.===
52 | %% @end
53 | %%-------------------------------------------------------------------------
54 |
55 | -spec box_muller(Mean :: number(),
56 | StdDev :: number()) ->
57 | {Result1 :: float(), Result2 :: float()}.
58 |
59 | box_muller(Mean, StdDev) ->
60 | X1 = quickrand_cache:floatR(),
61 | X2 = ?PI2 * quickrand_cache:floatR(),
62 | K = StdDev * math:sqrt(-2.0 * math:log(X1)),
63 | Result1 = Mean + K * math:cos(X2),
64 | Result2 = Mean + K * math:sin(X2),
65 | {Result1, Result2}.
66 |
67 | %%-------------------------------------------------------------------------
68 | %% @doc
69 | %% ===State cache version of quickrand_normal:box_muller/2.===
70 | %% @end
71 | %%-------------------------------------------------------------------------
72 |
73 | -spec box_muller(Mean :: number(),
74 | StdDev :: number(),
75 | State0 :: quickrand_cache:state()) ->
76 | {Result1 :: float(), Result2 :: float(), StateN :: quickrand_cache:state()}.
77 |
78 | box_muller(Mean, StdDev, State0) ->
79 | {X1, State1} = quickrand_cache:floatR(State0),
80 | {R2, StateN} = quickrand_cache:floatR(State1),
81 | X2 = ?PI2 * R2,
82 | K = StdDev * math:sqrt(-2.0 * math:log(X1)),
83 | Result1 = Mean + K * math:cos(X2),
84 | Result2 = Mean + K * math:sin(X2),
85 | {Result1, Result2, StateN}.
86 |
87 | %%%------------------------------------------------------------------------
88 | %%% Private functions
89 | %%%------------------------------------------------------------------------
90 |
91 |
--------------------------------------------------------------------------------
/doc/random_wh82.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Module random_wh82
6 |
7 |
8 |
9 |
25 | The random numbers created by the functions in this module are
26 | not meant for cryptographic purposes.
27 |
28 |
Any functions that have a jenkins prefix use Bob Jenkins' lookup3 hashing
29 | (lookup3, May 2006). In my testing, the function jenkins_32 is 4 times
30 | slower than erlang:phash2/1 because jenkins_32 is implemented in Erlang.
31 | Both the jenkins_32 and jenkins_64 functions execute at a speed similar to
32 | crypto:hash(ripemd160,_) with crypto:hash(sha256,_) slightly faster and
33 | crypto:hash(sha512,_) slightly slower.
34 |
35 |
Any functions that have a jenkins64 prefix use Bob Jenkins' SpookyHash
36 | (SpookyV2, August 5 2012). In my testing, the function jenkins64_128 is
37 | 4.5 times slower than crypto:hash(md5,_) which provides the same number
38 | of bits, because the jenkins64 functions are implemented in Erlang.
39 |
40 |
The jenkins prefix functions are faster than the jenkins64 prefix functions
41 | due to avoiding Erlang bignums and both provide the same quality.
42 |
43 | All the functions have been checked with the C++ implementations to ensure
44 | the same hash value is obtained, though this implementation forces numbers
45 | to be interpreted as big-endian.
46 |
15 | Use this module for data from crypto:strong_rand_bytes/1 while avoiding
16 | the high latency normally associated with crypto:strong_rand_bytes/1
17 | usage. Data is cached to minimize the latency from each
18 | crypto:strong_rand_bytes/1 function call.
19 |
20 | The cache_size option may be provided to either the init/1 function or
21 | the new/1 function to adjust the amount of data cached.
22 |
30 | Use this module for data from crypto:strong_rand_bytes/1 while avoiding
31 | the high latency normally associated with crypto:strong_rand_bytes/1
32 | usage. Data is cached to minimize the latency from each
33 | crypto:strong_rand_bytes/1 function call.
34 |
35 | The cache_size option may be provided to either the init/1 function or
36 | the new/1 function to adjust the amount of data cached. The cache_size
37 | value should vary based on the amount of random data consumed for a
38 | single function call and can be set based on higher-level system testing.
39 | If the cache_size option is not provided, the default set in the
40 | quickrand Erlang/OTP application env cache_size configuration parameter
41 | is used (64 KB is the default setting).
42 |
33 | Both algorithms used for the variables LCG and MCG provide
34 | fast low-quality pseudo-random number generation without using
35 | Erlang bignums.
T = A * X0 + C0
39 | X1 = Y0
40 | Y1 = Z0
41 | C1 = T bsr 64
42 | Z1 = T band 16#ffffffffffffffff
43 | A = 16#ff377e26f82da74a, 0 < X0, 0 < Y0, 0 < Z0, 0 < C0 < A - 1
44 |
45 | Simulates a multiplicative LCG with prime modulus
46 | M = 16#ff377e26f82da749ffffffffffffffffffffffffffffffffffffffffffffffff .
47 | The period is approximately 2^255.
48 | Vigna, Sebastiano.
49 | https://prng.di.unimi.it/MWC256.c
50 | https://prng.di.unimi.it/#quality
51 | TestU01 BigCrush passed (p-value statistics are in [0.001..0.999])
52 | when starting from 100 equispaced points of the state space.
T = A * X0 + C0
64 | C1 = T bsr 32
65 | X1 = T band 16#ffffffff
66 | A = 16#7fa6502, 0 < X0, 0 < C0 < A - 1
67 |
68 | Simulates a multiplicative LCG with prime modulus
69 | M = 16#7fa6501ffffffff (M = A * 2^32 - 1).
70 | The period is approximately 2^58.
71 | X1 and C1 are combined with xor to produce a 32-bit random number.
72 | TestU01 SmallCrush/Crush/BigCrush have been used to test the 32-bit result
73 | (both with the bits forward and reversed)
74 | and the p-value statistics are in [0.0000001..0.9999999]
75 | (when starting from 100 equispaced points of the state space).
76 | The wider bounds (i.e., wider than [0.001..0.999]) are due to the
77 | shorter period.
80 | Backwards-compatible seeding of random number generators for this
81 | module's uniform prefix functions and the external modules used
82 | (rand, random_wh06_int and random_wh82).
83 | Use seed/1 to seed specific random number generators.
84 |
85 | Instead of using this function, it is better to use a jump function
86 | for obtaining non-overlapping sequences, if a jump function is available
87 | and the number of Erlang processes used is limited
88 | (to ensure concurrent usage of the same algorithm has no collisions).
91 | Instead of using this function, it is better to use a jump function
92 | for obtaining non-overlapping sequences, if a jump function is available
93 | and the number of Erlang processes used is limited
94 | (to ensure concurrent usage of the same algorithm has no collisions).
129 | Both algorithms used for the variables LCG and MCG provide
130 | fast low-quality pseudo-random number generation without using
131 | Erlang bignums.
132 |
LCG:
133 | 35-bit classical Linear Congruential Generator
134 | based on Erlang/OTP 25.0-rc3 rand:lcg35/1.
135 |
136 | X1 = (A * X0 + C) rem M
137 | A = 15319397, C = 15366142135, M = 2^35
138 |
139 | C is an odd value close to M / sqrt(5).
140 | The period is M (i.e., 2^35).
141 |
142 | MCG:
143 | 35-bit Multiplicative Congruential Generator
144 | (i.e., Lehmer random number generator,
145 | Park-Miller random number generator)
146 | based on Erlang/OTP 25.0-rc3 rand:mcg35/1.
147 |
148 | X1 = (A * X0) rem M
149 | A = 185852, B = 35, D = 31, M = 2^B - D
150 |
151 | D makes M prime (M == 34359738337) so X0 is always coprime.
152 | The period is M (i.e., 2^35 - 31).
153 | The LCG and MCG are combined with xor to produce a 32-bit random number.
154 | TestU01 SmallCrush/Crush/BigCrush have been used to test the 32-bit result
155 | (both with the bits forward and reversed)
156 | and the p-value statistics are in [0.0000001..0.9999999]
157 | (when starting from 100 equispaced points of the state space).
158 | The wider bounds (i.e., wider than [0.001..0.999]) are due to the
159 | shorter period.
160 |
161 |
mwc59x_32/1 is slighly more efficient but provides slightly less randomness
162 | (same p-value statistics bounds but the separate sums of
163 | (1e-8 .. 1e-4] and [1 - 1e-4 .. 1 - 1e-8) are less extreme
164 | for lcg35x_32/1, i.e., the mwc59x_32/1 (1e-8 .. 1e-4] sum is 25.5% smaller
165 | and the mwc59x_32/1 [1 - 1e-4 .. 1 - 1e-8) sum is 16.1% larger while
166 | mwc59x_32/1 provides roughly a 1.08x speedup with Erlang/OTP 25.0).
167 |
168 |
Pierre L'Ecuyer, Richard Simard.
169 | TestU01: A C Library for Empirical Testing of Random Number Generators.
170 | ACM Transactions on Mathematical Software, vol. 33, iss. 4, article 22, 2007.
171 | http://portal.acm.org/citation.cfm?doid=1268776.1268777
172 | http://simul.iro.umontreal.ca/testu01/tu01.html
173 |
174 | (A is selected from)
175 | L'Ecuyer, Pierre. Tables of linear congruential generators of
176 | different sizes and good lattice structure.
177 | Mathematics of Computation, vol. 68, no. 225, pp. 249–260, 1999.
178 | https://www.ams.org/journals/mcom/1999-68-225/S0025-5718-99-00996-5/
179 | https://www.iro.umontreal.ca/~lecuyer/myftp/papers/latrules99Errata.pdf
180 |
181 |
T = A * X0 + C0
188 | X1 = Y0
189 | Y1 = Z0
190 | C1 = T bsr 64
191 | Z1 = T band 16#ffffffffffffffff
192 | A = 16#ff377e26f82da74a, 0 < X0, 0 < Y0, 0 < Z0, 0 < C0 < A - 1
193 |
194 | Simulates a multiplicative LCG with prime modulus
195 | M = 16#ff377e26f82da749ffffffffffffffffffffffffffffffffffffffffffffffff .
196 | The period is approximately 2^255.
197 | Vigna, Sebastiano.
198 | https://prng.di.unimi.it/MWC256.c
199 | https://prng.di.unimi.it/#quality
200 | TestU01 BigCrush passed (p-value statistics are in [0.001..0.999])
201 | when starting from 100 equispaced points of the state space.
T = A * X0 + C0
230 | C1 = T bsr 32
231 | X1 = T band 16#ffffffff
232 | A = 16#7fa6502, 0 < X0, 0 < C0 < A - 1
233 |
234 | Simulates a multiplicative LCG with prime modulus
235 | M = 16#7fa6501ffffffff (M = A * 2^32 - 1).
236 | The period is approximately 2^58.
237 | X1 and C1 are combined with xor to produce a 32-bit random number.
238 | TestU01 SmallCrush/Crush/BigCrush have been used to test the 32-bit result
239 | (both with the bits forward and reversed)
240 | and the p-value statistics are in [0.0000001..0.9999999]
241 | (when starting from 100 equispaced points of the state space).
242 | The wider bounds (i.e., wider than [0.001..0.999]) are due to the
243 | shorter period.
244 |
245 |
rand:mwc59/1 in Erlang/OTP 25.0 is similar. However, usage of rand:mwc59/1
246 | with rand:mwc59_value32/1 clearly fails the TestU01 Crush and BigCrush tests
247 | (e.g., with X0 and C0 initially set to 1). mwc59x_32/1 was created
248 | to provide more statistically significant randomness than is possible when
249 | using rand:mwc59/1 .
250 |
251 |
Pierre L'Ecuyer, Richard Simard.
252 | TestU01: A C Library for Empirical Testing of Random Number Generators.
253 | ACM Transactions on Mathematical Software, vol. 33, iss. 4, article 22, 2007.
254 | http://portal.acm.org/citation.cfm?doid=1268776.1268777
255 | http://simul.iro.umontreal.ca/testu01/tu01.html
267 | Backwards-compatible seeding of random number generators for this
268 | module's uniform prefix functions and the external modules used
269 | (rand, random_wh06_int and random_wh82).
270 | Use seed/1 to seed specific random number generators.
271 |
272 | Instead of using this function, it is better to use a jump function
273 | for obtaining non-overlapping sequences, if a jump function is available
274 | and the number of Erlang processes used is limited
275 | (to ensure concurrent usage of the same algorithm has no collisions).
276 |
277 |
283 | Instead of using this function, it is better to use a jump function
284 | for obtaining non-overlapping sequences, if a jump function is available
285 | and the number of Erlang processes used is limited
286 | (to ensure concurrent usage of the same algorithm has no collisions).
287 |
288 |