Three Methods of Generating Random Numbers in Erlang

Time:2019-3-26

Erlang has three ways to produce random numbers

random:uniform().

This function is provided by the random module of the Erlang library. This is generally used.

Copy codeThe code is as follows:
1> random:uniform().
0.4435846174457203

erlang:now().

Use the current time as a random belief that many people have done it. So if you don’t have too many requirements, of course you can do the same.

Copy codeThe code is as follows:
1> erlang:now().
{1419,831449,715000}

crypto:strong_rand_bytes(N).

Crypto module is used for encryption. The strong_rand_bytes function can generate uniform random numbers of N bytes. It returns binary data. as

Copy codeThe code is as follows:
1> <<A:32,B:32,C:32>> = crypto:strong_rand_bytes(12).
<<154,106,144,218,65,238,246,170,246,70,252,167>>
2> A.
2590675162

That’s shengcheng, three 32-bit random numbers, equivalent to three random integers.

Recommended Today

JS generate guid method

JS generate guid method https://blog.csdn.net/Alive_tree/article/details/87942348 Globally unique identification(GUID) is an algorithm generatedBinaryCount Reg128 bitsNumber ofidentifier , GUID is mainly used in networks or systems with multiple nodes and computers. Ideally, any computational geometry computer cluster will not generate two identical guids, and the total number of guids is2^128In theory, it is difficult to make two […]