Generating Gaussian Random Numbers

http://www.taygeta.com/random/gaussian.html

        This note is about the topic of generating

Gaussia 

        pseudo-random numbers given a source of

uniform 

        pseudo-random numbers. This topic comes up more frequently than I would have expected, so I decided to write this up on

one  of the best ways to do this. At the end of this note there is a list of references

        in the literature that are relevant to this topic. You can see some

code example

        that implement the technique, and a

tep-by-step

        example for generating

Weibull 

      distributed random numbers.

There are many ways of solving this problem (see for example Rubinstein, 1981, for an extensive discussion of this topic) but we will only go into one important method here. If we have an equation that describes our desired distribution function, then it is possible to use some mathematical trickery based upon the fundamental transformation law of probabilitiesto obtain a transformation function for the distributions. This transformation takes random variables from one distribution as inputs and outputs random variables in a new distribution function. Probably the most important of these transformation functions is known as the Box-Muller (1958) transformation. It allows us to transform uniformly distributed random variables, to a new set of random variables with a Gaussian (or Normal) distribution.

The most basic form of the transformation looks like:

         y1 = sqrt( - 2 ln(x1) ) cos( 2 pi x2 )
y2 = sqrt( - 2 ln(x1) ) sin( 2 pi x2 )
      We start with

two

      independent random numbers, x1 and x2, which come from a uniform distribution (in the range from 0 to 1). Then apply the above transformations to get two new independent random numbers which have a Gaussian distribution with zero mean and a standard deviation of one.

This particular form of the transformation has two problems with it,

      1. It is slow because of many calls to the math library.
      2. It can have numerical stability problems when x1 is very close to zero.

      These are serious problems if you are doing

stochastic modelling

      and generating millions of numbers.

The polar form of the Box-Muller transformation is both faster and more robust numerically. The algorithmic description of it is:

         float x1, x2, w, y1, y2;

         do {
x1 = 2.0 * ranf() - 1.0;
x2 = 2.0 * ranf() - 1.0;
w = x1 * x1 + x2 * x2;
} while ( w >= 1.0 ); w = sqrt( (-2.0 * log( w ) ) / w );
y1 = x1 * w;
y2 = x2 * w;
      where

ranf()

      is the routine to obtain a random number uniformly distributed in [0,1]. The polar form is faster because it does the equivalent of the sine and cosine geometrically without a call to the trigonometric function library. But because of the possiblity of many calls to

ranf()

      , the uniform random number generator should be fast (I generally recommend

R250

      for most applications).

Probability transformations for Non Gaussian distributions

      Finding transformations like the Box-Muller is a tedious process, and in the case of empirical distributions it is not possible. When this happens, other (often approximate) methods must be resorted to. See the reference list below (in particular

Rubinstein, 1981

      ) for more information.

There are other very useful distributions for which these probability transforms have been worked out. Transformations for such distributions as the Erlangexponential,hyperexponential, and the Weibull distribution can be found in the literature (see for example,MacDougall, 1987).


Useful References

  • Box, G.E.P, M.E. Muller 1958; A note on the generation of random normal deviates, Annals Math. Stat, V. 29, pp. 610-611
    1. Carter, E.F, 1994; The Generation and Application of Random Numbers, Forth Dimensions Vol XVI Nos 1 & 2, Forth Interest Group, Oakland California
    1. Knuth, D.E., 1981; The Art of Computer Programming, Volume 2 Seminumerical Algorithms, Addison-Wesley, Reading Mass., 688 pages, ISBN 0-201-03822-6
    1. MacDougall,M.H., 1987; Simulating Computer Systems, M.I.T. Press, Cambridge, Ma., 292 pages, ISBN 0-262-13229-X
    1. Press, W.H., B.P. Flannery, S.A. Teukolsky, W.T. Vetterling, 1986; Numerical Recipes, The Art of Scientific Computing, Cambridge University Press, Cambridge, 818 pages, ISBN 0-512-30811-9
  1. Rubinstein, R.Y., 1981; Simulation and the Monte Carlo method, John Wiley & Sons, ISBN 0-471-08917-6

See Also

      : A

Reference list

    of papers on Random Number Generation.

 Everett (Skip) Carter          Phone: 831-641-0645 FAX:  831-641-0647
Taygeta Scientific Inc. INTERNET: skip@taygeta.com
1340 Munras Ave., Suite 314 UUCP: ...!uunet!taygeta!skip
Monterey, CA. 93940 WWW: http://www.taygeta.com/

Taygeta's Home page

Generating Gaussian Random Numbers(转)的更多相关文章

  1. C++ Standard-Library Random Numbers

    Extracted from Section 17.4 Random Numbers, C++ Primer 5th. Ed. The random-number library generates ...

  2. Random Numbers Gym - 101466K dfs序+线段树

    Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...

  3. 2017 ACM-ICPC, Universidad Nacional de Colombia Programming Contest K - Random Numbers (dfs序 线段树+数论)

    Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...

  4. [Python] Generating random numbers using numpy lib

    import numpy as np def test_run(): data=np.random.random((3,4)) """ [[ 0.80150549 0.9 ...

  5. Random numbers

    Most computer programs do the same thing every time they execute, given the same inputs, so they are ...

  6. [Swift] 随机数 | Random numbers

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. [Functional Programming] Pull Many Random Numbers in a Single State ADT Transaction

    We have the ability to select a single random card from a pile of twelve cards, but we would like to ...

  8. Generating a Random Sample from discrete probability distribution

    If is a discrete random variable taking on values , then we can write . Implementation of this formu ...

  9. K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)

    题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...

随机推荐

  1. RxBinding -- 官网说明

    RxBinding -- 官网说明 新建 模板 小书匠 作用 组件 平台绑定 support-v4 绑定 appcompact-v7 绑定 design 库绑定 recyclerview-v7 绑定 ...

  2. 利用docker搭建测试环境--安装

    软件测试过程中,总会碰到测试环境不够用的尴尬情况.即时有了机器还要经历装系统,配置环境,调试等一系列繁琐的问题.虽然市面上也有一些批处理话的工具(如salt,fabric等),但是还是需要实体机器作为 ...

  3. 【vijos】1881 闪烁的繁星(线段树+特殊的技巧)

    https://vijos.org/p/1881 这场比赛太难了sad.所以我都没做.. 这题一开始我竟然不会sad(本来就不会),然后我继续yy..yy了好久,竟然yy了个什么可拆分的并查集?(sa ...

  4. 网易研发project师(移动端游戏)—暑期实习生电面题目 2014年5月14日

    2014年5月14日 暑期实习生电话面试: 首先自我介绍. 一.C++ 1.extern的使用方法 2.虚函数 3.强制转换 4.malloc和new的差别 二.计算机网络 1.TCP和UDP有什么差 ...

  5. asp.net正则表达式删除指定的HTML标签的代码

    抓取某网页的数据后(比如描述),如果照原样显示的话,可能会因为它里面包含没有闭合的HTML标签而打乱了格式,也可能它里面用了比较让人 费解 的HTML标签,把预订的格式搅乱. 如果全盘删除里面的 HT ...

  6. VSCode调试配置

    http://code.visualstudio.com/docs/editor/debugging#_launch-configurations VSCode内置Node.js运行时, 能调试jav ...

  7. TypeScript 接口(三)

    TypeScript的核心原则之一是对值所具有的结构进行类型检查. 接口初始: interface objProperty { name: string } function printName(na ...

  8. Linux 下添加用户,修改权限

    最近在用虚拟机,安装了centos,现在想新添加一个用户,在网上找了找方法.[原文链接] # useradd –d /usr/sam -m sam 此命令创建了一个用户sam,其中-d和-m选项用来为 ...

  9. 【原】storm源码之mac os x编译twitter storm源码

    twitter storm是由backtype公司创始人nathanmarz一手研发和开源的流计算(实时计算)框架,堪称实时计算领域的hadoop.nathanmarz也是在mac os x环境下开发 ...

  10. mysql5.7的密码

    [root@mysql ~]# grep "temporary password" /var/log/mysqld.log 2018-04-03T08:08:05.867624Z ...