randomintegerelixir

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?


Solution

  • 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