Archive for September, 2006

Random in Erlang

I was recently trying to fix our Erlang Jukebox (which Tony is still owing a Blog Post for (and once he’s done that I can write a blog post on adding m3u support to our Erlang Jukebox (and some time after that it should be opensourced…))) so that it would randomly enqueue the wrong track, just to spice things up. Erlang has a uniform function in the random module which returns a float uniformly distributed between 0 and 1. “Brilliant” said I.

However, the random module stores its state in the process dictionary which means that each process has a different state for the random module. It also seems to get seeded by preset values rather than by some more suitable time-based value. So, all works well within the same process:

> lists:foreach(fun(E) -> io:format("~w~n", [random:uniform()]) end,
                lists:seq(1,10)).
0.289971
0.186964
0.640637
0.553759
0.478947
0.457143
0.920461
0.915298
0.968977
0.903338
ok

But, once all the call to uniform happens fresh in each process, things are a little less ideally random:

> lists:foreach(fun(E) -> F = fun() ->
                                io:format("~w~n", [random:uniform()]) end,
                          spawn(F) end,
                lists:seq(1,10)).   
9.23009e-2
9.23009e-2
9.23009e-2
9.23009e-2
9.23009e-2
9.23009e-2
9.23009e-2
9.23009e-2
9.23009e-2
9.23009e-2
ok

And of course, in our Erlang Jukebox (yes, the same one Tony’s due to blog about), using the Erlang webserver Yaws, one process is spawned per HTTP request. So I needed to seed the random module with a time based value to make sure that overall, the behaviour was, well, random.

2 comments September 6th, 2006 matthew

Coming to an Interweb near you

Schmoogle, the search engine for finding industry events.

Really, this is imminent, if not already extant …

1 comment September 1st, 2006 mikeb

Next Posts

Calendar

September 2006
M T W T F S S
« Aug   Oct »
 123
45678910
11121314151617
18192021222324
252627282930  

Posts by Month

Posts by Category