> Erlang中文手册 > seed/3 用数字产生随机数种子

ranDOM:seed/3

用数字产生随机数种子

用法:

seed(A1, A2, A3) -> undefined | ran()

内部实现:

%% seed(A1, A2, A3) 
%%  Seed random number generation 

-spec seed(A1, A2, A3) -> 'undefined' | ran() when
      A1 :: integer(),
      A2 :: integer(),
      A3 :: integer().

seed(A1, A2, A3) ->
    seed_put({(abs(A1) rem (?PRIME1-1)) + 1,   % Avoid seed numbers that are
	      (abs(A2) rem (?PRIME2-1)) + 1,   % even divisors of the
	      (abs(A3) rem (?PRIME3-1)) + 1}). % corresponding primes.


-spec seed_put(ran()) -> 'undefined' | ran().
     
seed_put(Seed) ->
    put(random_seed, Seed).

用数字产生随机数种子

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