> Erlang中文手册 > uniform_s/1 返回一个随机浮点数

ranDOM:uniform_s/1

返回一个随机浮点数

用法:

uniform_s(State0) -> {float(), State1}

内部实现:

%% uniform_s(State) -> {F, NewState}
%%  Returns a random float between 0 and 1.

-spec uniform_s(State0) -> {float(), State1} when
      State0 :: ran(),
      State1 :: ran().

uniform_s({A1, A2, A3}) ->
    B1 = (A1*171) rem ?PRIME1,
    B2 = (A2*172) rem ?PRIME2,
    B3 = (A3*170) rem ?PRIME3,
    R = B1/?PRIME1 + B2/?PRIME2 + B3/?PRIME3,
    {R - trunc(R), {B1,B2,B3}}.

给定一个随机状态 State0, 并返回一个0.0 到 1.0 间的浮点数 float() 和一个新随机状态 State1.

{MegaSecs, Secs, MicroSecs} = erlang:now(),
State = {MegaSecs, Secs, MicroSecs},
random:uniform_s(State).