每次调用会更新进程字典里的random_seed变量,这样在同一个进程内每次调用random:uniform()时,随机数种子都不同,所以生成的随机数都不一样(调用完random:uniform()后,可以用get(random_seed)查看更新后的种子值)。

但是如果是不同的进程分别调用random:uniform(),因为随机种子更新的算法是一样的,所以每次各进程的随机数种子也是相同的,从而生成的随机数也是一样的,要想让不同进程生成的随机数不同,要手动为每个进程设置不同的种子,常用的是用erlang:now,比如:

random:seed(erlang:now()),random:uniform().

不过如果每个进程调用random:seed(erlang:now())太接近,种子值会比较接近,生成的随机数也会比较接近,更好的方法是用一个单独的进程来生成种子,保证每次的种子值相差比较大:

Seed = {random:uniform(99999), random:uniform(999999), random:uniform(999999)}

然后每次调用random:uniform()前从该种子生成进程获取最新的种子值,seed()之。

下面为random.erl 的源码:

 %%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2011. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
-module(random). %% Reasonable random number generator.
%% The method is attributed to B. A. Wichmann and I. D. Hill
%% See "An efficient and portable pseudo-random number generator",
%% Journal of Applied Statistics. AS183. 1982. Also Byte March 1987. -export([seed/0, seed/1, seed/3, uniform/0, uniform/1,
uniform_s/1, uniform_s/2, seed0/0]). -define(PRIME1, 30269).
-define(PRIME2, 30307).
-define(PRIME3, 30323). %%-----------------------------------------------------------------------
%% The type of the state -type ran() :: {integer(), integer(), integer()}. %%----------------------------------------------------------------------- -spec seed0() -> ran(). seed0() ->
{3172, 9814, 20125}. %% seed()
%% Seed random number generation with default values -spec seed() -> ran(). seed() ->
case seed_put(seed0()) of
undefined -> seed0();
{_,_,_} = Tuple -> Tuple
end. %% 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(A1, A2, A3). %% 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). %% uniform()
%% Returns a random float between 0 and 1. -spec uniform() -> float(). uniform() ->
{A1, A2, A3} = case get(random_seed) of
undefined -> seed0();
Tuple -> Tuple
end,
B1 = (A1*171) rem ?PRIME1,
B2 = (A2*172) rem ?PRIME2,
B3 = (A3*170) rem ?PRIME3,
put(random_seed, {B1,B2,B3}),
R = B1/?PRIME1 + B2/?PRIME2 + B3/?PRIME3,
R - trunc(R). %% uniform(N) -> I
%% Given an integer N >= 1, uniform(N) returns a random integer
%% between 1 and N. -spec uniform(N) -> pos_integer() when
N :: pos_integer(). uniform(N) when is_integer(N), N >= 1 ->
trunc(uniform() * N) + 1. %%% Functional versions %% 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}}. %% uniform_s(N, State) -> {I, NewState}
%% Given an integer N >= 1, uniform(N) returns a random integer
%% between 1 and N. -spec uniform_s(N, State0) -> {integer(), State1} when
N :: pos_integer(),
State0 :: ran(),
State1 :: ran(). uniform_s(N, State0) when is_integer(N), N >= 1 ->
{F, State1} = uniform_s(State0),
{trunc(F * N) + 1, State1}.

random:seed 由进程字典put存了随机数,random:uniform则get取了随机数,而它同时又put了新的随机数.

如果一开始直接调用,random:uniform/0,  则一开始 get(random_seed)为undefined,后面每次生产的种子的规则都是根据其业务规则生成。 但如果是 先调用random:seed/1 ,则它先生成了随机种子,put到random_seed 的进程字典中,后面依次调用random:uniform/0的时候,则从random_seed的进程字典中取出随机种子,则不是undefined,后面根据其业务规则,生成随机数.

erlang的随机数 及 random:uniform()函数的更多相关文章

  1. [Python] uniform() 函数

    描述uniform() 方法将随机生成下一个实数,它在[x, y) 范围内. 语法以下是 uniform() 方法的语法: import random random.uniform(x, y) 注意: ...

  2. python--随机函数(random,uniform,randint,randrange,shuffle,sample)

    random() random()方法:返回随机生成的一个实数,它在[0,1)范围内 运用random()方法的语法: import random #random()方法不能直接访问,需要导入rand ...

  3. random模块函数分析(一)

    random是python产生伪随机数的模块,随机种子默认为系统时钟.下面分析模块中的方法: 1.random.randint(start,stop): 这是一个产生整数随机数的函数,参数start代 ...

  4. python 随机数模块 -- random

    一.概述 这个模块实现的伪随机数生成器. 对于整数,从区间选取.对于序列,随机元素. 在实线的,有功能来计算均匀分布,正态分布(高斯) ,对数正态分布,负指数,γ和β分布.对于生成的角度分布,冯·米塞 ...

  5. numpy.random.uniform()

    numpy.random.uniform均匀分布 2018年06月19日 23:28:03 徐小妹 阅读数:4238   numpy.random.uniform介绍: 1. 函数原型:  numpy ...

  6. python模块 | 随机数模块—random模块

    python随机数模块 random - 生成伪随机数,该模块实现了各种分布的伪随机数生成器. 对于整数,从范围中有统一的选择. 对于序列,存在随机元素的统一选择.用于生成列表的随机排列的函数.以及用 ...

  7. 对抗生成网络-图像卷积-mnist数据生成(代码) 1.tf.layers.conv2d(卷积操作) 2.tf.layers.conv2d_transpose(反卷积操作) 3.tf.layers.batch_normalize(归一化操作) 4.tf.maximum(用于lrelu) 5.tf.train_variable(训练中所有参数) 6.np.random.uniform(生成正态数据

    1. tf.layers.conv2d(input, filter, kernel_size, stride, padding) # 进行卷积操作 参数说明:input输入数据, filter特征图的 ...

  8. js 获取随机数 Math.random()

    js 获取随机数 Math.random() // 结果为0-1间的一个随机数(包括0,不包括1) var randomNum1 = Math.random(); //console.log(rand ...

  9. Python random模块random/uniform/randint/choice/getrandbits/shuffle/choice/sample随机函数

    1.random.random() 返回0<=n<1之间的随机实数n 2. random.uniform() 弥补了上面函数的不足,它可以设定浮点数的范围,一个是上限,一个是下限. 3. ...

随机推荐

  1. DataBinder.Eval值的判断

    原文发布时间为:2009-04-10 -- 来源于本人的百度文章 [由搬家工具导入] 问:如何对<%# DataBinder.Eval(Container.DataItem,"Ly_R ...

  2. angular 右击事件的写法

    .directive('ngRightClick', function ($parse){ return function (scope, element, attrs){ var fn = $par ...

  3. PE文件格式---节和节表

    17.1.4  节表和节 从排列位置来看,PE文件在DOS部分和PE文件头部分以后就是节表和多个不同的节(如图17.1中的③和④所示).要理解什么是节表,什么是节以及它们之间的关系,那就首先要了解Wi ...

  4. 将一个二叉树左右翻转(Java 版)

    public class Solution { public TreeNode invertTree(TreeNode root) { if (root == null) { return null; ...

  5. Windows开发

    1. 介绍 这里简单介绍了Windows应用程序开发的基础知识 2. 基础 Windows下的应用程序有控制台程序和Win32窗口程序,这里讲的是Win32窗口程序 Windows提供了相关静态库(L ...

  6. Syslinux使用

    1. 介绍 Syslinux是一个功能强大的引导加载程序, 可以装在U盘上来引导系统 在5.00版本以前,几乎所有c32模块是独立的,即没有其他模块依赖:但在5.00以后,很多c32模块则是依赖于其他 ...

  7. linux下kodi没有声音的解决

    前几天,心血来潮,就安装了manjaro的pre3版本,由于是mini kde版本的,就随手安装了kodi,可以用来看视频,听音乐和看图片. 结果在所有插件都折腾好了之后发现,在屏幕的右上角有一个喇叭 ...

  8. Chrome扩展修改页面代码执行环境的方法

    Chrome的扩展程序可以通过content scripts向页面中注入js代码,所注入的js代码能够对页面中所有的DOM对象进行操作.由于Chrome在js执行环境上对页面代码和content sc ...

  9. webview reload 错误 Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted"

    在某个特定的场合先需要对WKWebView进行一次reload,但是直接回走到失败的代理方法中并报如下的错误 Error Domain=WebKitErrorDomain Code=102 " ...

  10. Codeforces Round #449 (Div. 2) B. Chtholly's request【偶数位回文数】

    B. Chtholly's request time limit per test 2 seconds memory limit per test 256 megabytes input standa ...