|
| 1 | +%% The contents of this file are subject to the Mozilla Public License |
| 2 | +%% Version 1.1 (the "License"); you may not use this file except in |
| 3 | +%% compliance with the License. You may obtain a copy of the License |
| 4 | +%% at http://www.mozilla.org/MPL/ |
| 5 | +%% |
| 6 | +%% Software distributed under the License is distributed on an "AS IS" |
| 7 | +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 8 | +%% the License for the specific language governing rights and |
| 9 | +%% limitations under the License. |
| 10 | +%% |
| 11 | +%% The Original Code is RabbitMQ. |
| 12 | +%% |
| 13 | +%% The Initial Developer of the Original Code is GoPivotal, Inc. |
| 14 | +%% Copyright (c) 2016 Pivotal Software, Inc. All rights reserved. |
| 15 | +%% |
| 16 | + |
| 17 | +-module(rand_compat). |
| 18 | + |
| 19 | +%% We don't want warnings about the use of erlang:now/0 in |
| 20 | +%% this module. |
| 21 | +-compile(nowarn_deprecated_function). |
| 22 | + |
| 23 | +%% Declare versioned functions to allow dynamic code loading, |
| 24 | +%% depending on the Erlang version running. See 'code_version.erl' for details |
| 25 | +-erlang_version_support([ |
| 26 | + {18, [ |
| 27 | + {seed, 1, seed_pre_18, seed_post_18}, |
| 28 | + {seed, 2, seed_pre_18, seed_post_18}, |
| 29 | + {uniform, 0, uniform_pre_18, uniform_post_18}, |
| 30 | + {uniform, 1, uniform_pre_18, uniform_post_18}, |
| 31 | + {uniform_s, 1, uniform_s_pre_18, uniform_s_post_18}, |
| 32 | + {uniform_s, 2, uniform_s_pre_18, uniform_s_post_18} |
| 33 | + ]} |
| 34 | + ]). |
| 35 | + |
| 36 | +-export([ |
| 37 | + seed/1, seed_pre_18/1, seed_post_18/1, |
| 38 | + seed/2, seed_pre_18/2, seed_post_18/2, |
| 39 | + uniform/0, uniform_pre_18/0, uniform_post_18/0, |
| 40 | + uniform/1, uniform_pre_18/1, uniform_post_18/1, |
| 41 | + uniform_s/1, uniform_s_pre_18/1, uniform_s_post_18/1, |
| 42 | + uniform_s/2, uniform_s_pre_18/2, uniform_s_post_18/2 |
| 43 | + ]). |
| 44 | + |
| 45 | +-define(IS_ALG(A), (A =:= exs64 orelse A =:= exsplus orelse A =:= exs1024)). |
| 46 | + |
| 47 | +%% export_seed_s/1 can't be implemented with `random`. |
| 48 | +%% export_seed_s/2. can't be implemented with `random`. |
| 49 | + |
| 50 | +%% normal_s/1 can't be implemented with `random`. |
| 51 | +%% normal_s/2. can't be implemented with `random`. |
| 52 | + |
| 53 | +%% seed/1. |
| 54 | + |
| 55 | +seed(AlgOrExpState) -> |
| 56 | + code_version:update(?MODULE), |
| 57 | + ?MODULE:seed(AlgOrExpState). |
| 58 | + |
| 59 | +seed_pre_18(Alg) when ?IS_ALG(Alg) -> random:seed(); |
| 60 | +seed_pre_18(ExpState) -> random:seed(ExpState). |
| 61 | +seed_post_18(AlgOrExpState) -> rand:seed(AlgOrExpState). |
| 62 | + |
| 63 | +%% seed/2. |
| 64 | + |
| 65 | +seed(Alg, ExpState) -> |
| 66 | + code_version:update(?MODULE), |
| 67 | + ?MODULE:seed(Alg, ExpState). |
| 68 | + |
| 69 | +seed_pre_18(_Alg, ExpState) -> random:seed(ExpState). |
| 70 | +seed_post_18(Alg, ExpState) -> rand:seed(Alg, ExpState). |
| 71 | + |
| 72 | +%% seed_s/1 can't be implemented with `random`. |
| 73 | +%% seed_s/2. can't be implemented with `random`. |
| 74 | + |
| 75 | +%% uniform/0. |
| 76 | + |
| 77 | +uniform() -> |
| 78 | + code_version:update(?MODULE), |
| 79 | + ?MODULE:uniform(). |
| 80 | + |
| 81 | +uniform_pre_18() -> random:uniform(). |
| 82 | +uniform_post_18() -> rand:uniform(). |
| 83 | + |
| 84 | +%% uniform/1. |
| 85 | + |
| 86 | +uniform(N) -> |
| 87 | + code_version:update(?MODULE), |
| 88 | + ?MODULE:uniform(N). |
| 89 | + |
| 90 | +uniform_pre_18(N) -> random:uniform(N). |
| 91 | +uniform_post_18(N) -> rand:uniform(N). |
| 92 | + |
| 93 | +%% uniform_s/1. |
| 94 | + |
| 95 | +uniform_s(State) -> |
| 96 | + code_version:update(?MODULE), |
| 97 | + ?MODULE:uniform_s(State). |
| 98 | + |
| 99 | +uniform_s_pre_18(State) -> random:uniform_s(State). |
| 100 | +uniform_s_post_18(State) -> rand:uniform_s(State). |
| 101 | + |
| 102 | +%% uniform_s/2. |
| 103 | + |
| 104 | +uniform_s(N, State) -> |
| 105 | + code_version:update(?MODULE), |
| 106 | + ?MODULE:uniform_s(N, State). |
| 107 | + |
| 108 | +uniform_s_pre_18(N, State) -> random:uniform_s(N, State). |
| 109 | +uniform_s_post_18(N, State) -> rand:uniform_s(N, State). |
0 commit comments