Giter Club home page Giter Club logo

quickrand's Introduction

Quick Random Number Generation

Build Status

Provides a simple interface to call efficient random number generation functions based on the context. Proper random number seeding is enforced.

random_wh82 is provided as an alternative to the random module which is scheduled to be removed in Erlang/OTP 20.

Build

rebar compile

Author

Michael Truog (mjtruog at protonmail dot com)

Thanks

  • Raimo Niskanen (Box-Muller transformation floating-point period)
  • Richard O'Keefe (floating-point random number period insights)

License

MIT License

quickrand's People

Contributors

getong avatar jlouis avatar okeuday avatar ratelle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

quickrand's Issues

unknown dialyzer warning option: no_underspecs

Hi. I have a compilation error:

_build/default/lib/quickrand/src/quickrand_hash.erl:80: unknown dialyzer warning option: no_underspecs

I have Erlang/OTP 24 and Dialyzer version v4.3 but the argument no_underspecs has been supported since version 4.4.

1> erlang:system_info(otp_release).
"24"
...
$> dialyzer --version
Dialyzer version v4.3

quickrand:seed/0 fails on <R20

Hello!

It looks like quickrand:seed/0 doesn't work on OTP versions less than 20.

This is because quickrand:seed_rand/3 attempts to call rand:seed/2 using the exsp parameter that is not defined in OTP < 20. (This is src/quickrand.erl:373)

There is an ifdef in place to try and catch this situation, but it's currently short-circuited to true. Perhaps a commit got lost somewhere along the way?

Global state instead of per process state [ENHANCEMENT]

Hi Michael

Is there any downside to cache the state within quickrand_cache:init/1 in a global cache?

_ = erlang:put(?TUPLE_PDICT_KEY, state_to_tuple(new(Options))),

Something along these lines quickrand_cache:init_/1:

-spec init_(Options :: options()) ->
    ok.
init_(Options) ->
    persistent_term:put(?TUPLE_PDICT_KEY, state_to_tuple(new(Options))).

Then, add this new quickrand_cache:rand_bytes_/1:

-spec rand_bytes_(N :: pos_integer()) ->
    binary().

rand_bytes_(N)
    when is_integer(N), N > 0 ->
    {I, CacheSize, Cache} = persistent_term:get(?TUPLE_PDICT_KEY),
    {Bytes, NewI, NewCache} = bytes_get(N, I, CacheSize, Cache),
    %% !!! don't know what can be done here as persistent_term:put/2 is really slow (triggers GC) !!!
    ok = persistent_term:put(?TUPLE_PDICT_KEY, {NewI, CacheSize, NewCache}),
    Bytes.

My application is heavily using your uuid module to generate v4 UUIDs.
While checking your code today i realized that you've added a nice caching layer to it.

I'm generating about 10 million v4 UUIDs per day and would like to switch to uuid:get_v4(cached_):

get_v4(cached) ->
    <<Rand1:48, _:4, Rand2:12, _:2, Rand3:62>> =
        quickrand_cache:rand_bytes_(16),
    <<Rand1:48,
      0:1, 1:1, 0:1, 0:1,  % version 4 bits
      Rand2:12,
      1:1, 0:1,            % RFC 4122 variant bits
      Rand3:62>>;

With this in place, all my processes will use the same cached state and will avoid to store extra data in their process dictionnary:

ok = quickrand_cache:init_([{cache_size, 16}]),
[...]
V4 = uuid:get_v4(cached),

Any issue with this?

Thanks.

R19 crypto error

src/quickrand.erl:89: crypto:rand_bytes/1 is deprecated and will be removed in in a future release; use crypto:strong_rand_bytes/1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.