The reality of Stefan Scholl

"I hope there's pudding"

Sad day on freenode


23:18 -christel(i=christel@freenode/staff/gentoo.christel)- [Global Notice]
On the 12th September Rob Levin, known to many as
Freenode's lilo, was hit by a car while riding his bike. He
suffered head injuries and passed away in hospital on the
16th. For more information please visit #freenode-announce


Rest in Peace, lilo. :-(



Tags: irc, freenode
Comments

Random in Erlang processes

I'm sure the people at LShift don't need my help, but it was fun playing with Erlang and finding one possible solution for the Random in Erlang problem.

Init the random process with random_p:start(). Then call random_p:uniform() instead of random:uniform() and you get different numbers, even when you use it from multiple, fresh spawned processes.



%%% Solution for http://www.lshift.net/blog/2006/09/06/random-in-erlang
%%%
%%% 2006-09-13 Stefan Scholl
%%%
%%% Public Domain (sic!)
%%%

-module(random_p).
-export([start/0, stop/0, uniform/0, generator/0]).
-author('stesch@no-spoon.de').


% Start the random-generator process
start() ->
Pid = spawn(?MODULE, generator, []),
register(random_p_generator, Pid),
random_p_generator ! seed.


% Process shutdown
stop() ->
random_p_generator ! shutdown.


% The actual process with the random state
generator() ->
receive
seed ->
{A1, A2, A3} = now(),
random:seed(A1, A2, A3),
generator();

shutdown ->
true;

{next_random, Pid} ->
Pid ! random:uniform(),
generator()
end.


% Getting the next uniform random number
uniform() ->
random_p_generator ! {next_random, self()},
receive
Random ->
Random
end.
Tags: Erlang
Comments