iilyak commented on a change in pull request #860: Remove usage of deprecated random module. URL: https://github.com/apache/couchdb/pull/860#discussion_r142407313
########## File path: src/couch/src/couch_rand.erl ########## @@ -0,0 +1,63 @@ +% Licensed under the Apache License, Version 2.0 (the "License"); you may not +% use this file except in compliance with the License. You may obtain a copy of +% the License at +% +% http://www.apache.org/licenses/LICENSE-2.0 +% +% Unless required by applicable law or agreed to in writing, software +% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +% License for the specific language governing permissions and limitations under +% the License. + +-module(couch_rand). + + +-export([ + uniform/0, + uniform/1 +]). + + +-compile({nowarn_deprecated_function, [ + {random, seed, 1}, + {random, uniform, 0}, + {random, uniform, 1} +]}). + + +uniform() -> + case erlang_erts_major_version() of + Ver when Ver >=7 -> % Erlang 18.0+ + rand:uniform(); + _ -> + maybe_set_random_seed(), + random:uniform() + end. + + +uniform(N) -> + case erlang_erts_major_version() of + Ver when Ver >= 7 -> % Erlang 18.0+ + rand:uniform(N); + _ -> + maybe_set_random_seed(), + random:uniform(N) + end. + + +maybe_set_random_seed() -> + case get(random_seed) of + undefined -> + {_, Sec, USec} = os:timestamp(), + Seed = {erlang:phash2(self()), Sec, USec}, + random:seed(Seed); + _ -> + ok + end. + + +erlang_erts_major_version() -> + ErtsVersion = erlang:system_info(version), Review comment: It looks like rand module is pure erlang. So it doesn't depend on erts version but rather on otp version. Would `erlang:system_info(otp_release).` be a better choice. Crazy thought can we include `rand.erl` (ASF2) into release for older erlang versions? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
