<div>Another way to generate uuids is to have a permanent gen_server process generate random strings. Having a long running process ensures good randomness since random:seed uses the process dictionary. Below is a random alphanumeric string generation function that you could plug in to a gen_server that calls random:seed at init time.</div>
<div><br></div><div><div>random(Size) -></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Chars = lists:seq($0, $9) ++ lists:seq($A, $Z) ++ lists:seq($a, $z),</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>N = length(Chars),</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>F = fun(_, S) -> [lists:nth(random:uniform(N), Chars) | S] end,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lists:foldl(F, [], lists:seq(1, Size)).</div>
</div><br><div class="gmail_quote"><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Date: Fri, 6 Jun 2008 11:25:32 -0400<br>
From: Andy Gross <<a href="mailto:andy@andygross.org">andy@andygross.org</a>><br>
Subject: Re: [erlang-questions] Use of uuidgen with a mnesia<br>
application<br>
To: <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
Message-ID: <<a href="mailto:E537F41E-913F-46A3-A3BD-13901D6D32B9@andygross.org">E537F41E-913F-46A3-A3BD-13901D6D32B9@andygross.org</a>><br>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes<br>
<br>
<br>
You can also use the following method for generating unique identifiers:<br>
<br>
1> crypto:start().<br>
ok<br>
2> <<I:160/integer>> = crypto:sha(term_to_binary({make_ref(), now()})).<br>
<41,13,88,29,5,111,251,80,169,13,157,98,38,200,99,138,10,<br>
166,160,63>><br>
3> UUIDString = erlang:integer_to_list(I, 16).<br>
"4EDB9BAAB77AF8612C14390672C9D9B9BFB3F32A"<br>
4><br>
<br>
- Andy<br>
<br>
On Jun 6, 2008, at 11:12 AM, Paul Fisher wrote:<br>
<br>
> On Fri, 2008-06-06 at 17:06 +0200, Alexander Lamb wrote:<br>
>> I see, but it means you wrote a piece of code in C (probably) to call<br>
>> uuid on the underlying system?<br>
>><br>
>> (maybe it's the word "drive" that I didn't fully understand.<br>
><br>
> Yes, a linked-in erlang driver, written in C. Once you write the<br>
> first<br>
> one it is not so bad after that.<br>
><br>
><br>
> --<br>
> paul<br></blockquote></div>