programing

How to generate a random number in Elixir?

sourcetip 2021. 1. 17. 12:38
반응형

How to generate a random number in Elixir?


I need to generate a random number. I found the Enum.random/1 function, but it expects an enumerable such as a list or range of numbers.

Is that the only way to get a random number?


You can call Erlang's rand module from Elixir code seamlessly.

random_number = :rand.uniform(n)

Will give a random number from 1 <= x <= n


&Enum.random/1

Enum.random(0..n) will generate 0 to n randomly

you can send list as argument too

ReferenceURL : https://stackoverflow.com/questions/38778054/how-to-generate-a-random-number-in-elixir

반응형