maillist有人提供了一种简单的UUID生成方法:
[quote]1> crypto:start().
ok
2> <<I:160/integer>> = crypto:sha(term_to_binary({make_ref(), now()})).
<<41,73,124,217,87,62,82,238,224,153,122,195,244,246,157,
142,88,71,76,61>>
3> lists:flatten(io_lib:fwrite("~40..0s", [erlang:integer_to_list(I, 16)])).
"29497CD9573E52EEE0997AC3F4F69D8E58474C3D"[/quote]
对于要求不太严格的场合,Erlang内部使用make_ref()是更好的选择
[quote]make_ref() -> ref()
Returns an almost unique reference.
The returned reference will re-occur after approximately 2^82 calls; therefore it is unique enough for practical purposes.
> make_ref().
#Ref<0.0.0.135>[/quote]
直接使用uuidgen命令也是常见的方法,注意这里末尾有个换行符
[quote]
1> os:cmd("uuidgen").
"7520c8bf-1517-416e-841f-993429ef1577\n"[/quote]
[quote]1> crypto:start().
ok
2> <<I:160/integer>> = crypto:sha(term_to_binary({make_ref(), now()})).
<<41,73,124,217,87,62,82,238,224,153,122,195,244,246,157,
142,88,71,76,61>>
3> lists:flatten(io_lib:fwrite("~40..0s", [erlang:integer_to_list(I, 16)])).
"29497CD9573E52EEE0997AC3F4F69D8E58474C3D"[/quote]
对于要求不太严格的场合,Erlang内部使用make_ref()是更好的选择
[quote]make_ref() -> ref()
Returns an almost unique reference.
The returned reference will re-occur after approximately 2^82 calls; therefore it is unique enough for practical purposes.
> make_ref().
#Ref<0.0.0.135>[/quote]
直接使用uuidgen命令也是常见的方法,注意这里末尾有个换行符
[quote]
1> os:cmd("uuidgen").
"7520c8bf-1517-416e-841f-993429ef1577\n"[/quote]