Using Erlang Random in Elixir Script Always Returns Same Number

1,649 views
Skip to first unread message

Riza Fahmi

unread,
Jan 16, 2014, 2:34:31 AM1/16/14
to elixir-l...@googlegroups.com
Hi all,

I'm trying to use erlang module random in elixir script. But everytime I run it (elixir random.exs), it always return the same number. Trying it on REPL is normal, it  randomize. Am I doing it wrong? This is the code:

defmodule Randomize do
def random(number) do
:random.uniform(number)
end
end
 
IO.inspect Randomize.random(10)

José Valim

unread,
Jan 16, 2014, 2:41:37 AM1/16/14
to elixir-l...@googlegroups.com
You need to give it a seed. Check the documentation for the seed function here:


Otherwise it uses a default seed (if I remember correctly).



José Valim
Skype: jv.ptec
Founder and Lead Developer


--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Benjamin Tan

unread,
Jan 16, 2014, 3:03:20 AM1/16/14
to elixir-l...@googlegroups.com
Try this:

defmodule Randomize do
def random(number) do
    :random.seed(:erlang.now)
:random.uniform(number)
end
end

Riza Fahmi

unread,
Jan 16, 2014, 3:09:33 AM1/16/14
to elixir-l...@googlegroups.com
Oh, ok I didn't know that. Thanks for figuring out for me :)

This is the revision using seed.

defmodule Randomize do
def random(number) do
:random.seed(:erlang.now())
:random.uniform(number)
end
end
 
IO.inspect Randomize.random(10)



photo
Riza Fahmi
Author ElixirDose
--
--riza

Dave Cottlehuber

unread,
Jan 16, 2014, 3:23:59 AM1/16/14
to elixir-l...@googlegroups.com
There's no need/point to reseed every time you ask for a random
number, arguably it could decrease your actual randomness
(IANACryptographer). I'd go one further and add an `on_load` attribute
to your module, and initialise the seed there. on_load specifies a
function to be called only when your module is loaded the first time,
or later if you do hot-code reloading. So, its more or less automatic.

I've got no computer atm, so something like this should work:

defmodule Randomise do
@on_load :reseed_generator

def reseed_generator do
:random.seed(:os.timestamp())
end

def random(number) do
:random.uniform(number)
end
end

Look at http://elixir-lang.org/docs/stable/Module.html and
http://www.erlang.org/doc/reference_manual/code_loading.html for more
info.

A+
Dave






















On 16 January 2014 09:09, Riza Fahmi <riza...@gmail.com> wrote:
> Oh, ok I didn't know that. Thanks for figuring out for me :)
>
> This is the revision using seed.
>
>
>
>
> defmodule Randomize do
> def random(number) do
> :random.seed(:erlang.now())
> :random.uniform(number)
> end
> end
>
>
>
> IO.inspect Randomize.random(10)
>
>
>
> Riza Fahmi
> Author ElixirDose
> | e: riza...@gmail.com | w: http://www.elixirdose.com
> Designed with WiseStamp - Get yours
>
>
>
> On Thu, Jan 16, 2014 at 2:41 PM, José Valim
> <jose....@plataformatec.com.br> wrote:
>>
>> You need to give it a seed. Check the documentation for the seed function
>> here:
>>
>> http://www.erlang.org/doc/man/random.html
>>
>> Otherwise it uses a default seed (if I remember correctly).
>>
>>
>>
>> José Valim
>> www.plataformatec.com.br
>> Skype: jv.ptec
>> Founder and Lead Developer
>>
>>
>> On Thu, Jan 16, 2014 at 8:34 AM, Riza Fahmi <riza...@gmail.com> wrote:
>>>
>>> Hi all,
>>>
>>> I'm trying to use erlang module random in elixir script. But everytime I
>>> run it (elixir random.exs), it always return the same number. Trying it on
>>> REPL is normal, it randomize. Am I doing it wrong? This is the code:
>>>
>>>
>>>
>>> defmodule Randomize do
>>> def random(number) do
>>>
>>> :random.uniform(number)
>>>
>>>
>>>
>>>
>>>
>>> end
>>> end
>>>
>>>
>>>
>>>
>>>
>>>
>>> IO.inspect Randomize.random(10)
>>>
>>>
>>>
>>>
>>>
>>>
>>> https://gist.github.com/rizafahmi/8451050#file-random
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> Riza Fahmi
>>> Author ElixirDose
>>> | e: riza...@gmail.com | w: http://www.elixirdose.com
>>>
>>> Get a signature like this: Grab yours!
>>>
Reply all
Reply to author
Forward
0 new messages