Erlang - Random Number Generator with Makeref

I am trying to quickly create a random number.

Now I am using the following:

uniqueID() ->   C = random:uniform(9999) ,   %%%DO SPEED TEST
    random:seed(C,random:uniform(99),random:uniform(99)),
    {_, {H, Min, S}}  = calendar:universal_time(),
    {A, B} = statistics(wall_clock),
    (A*B) +((H + C + Min) * S).

This takes too much time compared to something like make_ref ().

6> make_ref().
#Ref<0.0.0.74>

How can I take a unique ref and parse it as a whole?

e.g. 00074

Thanks for the help.

+5
source share
3 answers

Are you sure you want to use erlang:make_ref/0for unique numbers? Refs are unique to only one run of one erlang vm - they are repeatable, predictable and make lousy unique identifiers if you plan to use them for anything other than tags in erlang messages to match requests and responses.

, ref (erlang:ref_to_list/1), .

, , crypto:rand_bytes/1 N crypto:rand_uniform/2, . , (. openssl rand_bytes documentation).

+8

{A, B, C} = now(), A * 1000000000000 + B * 1000000 + C.

+1

Erlang/OTP release 18.0 , erlang:unique_integer/{0,1}.

, , , .

erlang: unique_integer() → integer()
erlang: unique_integer (ModList) → integer()     Modlist = []
    = |

, . , BIF, , , . .

:


    .


    , .

Since it has erlang:now/0become obsolete, several things are proposed to be implemented using erlang:unique_integer/{0,1}.

0
source

All Articles