Generating Gaussian Random Numbers(转)
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
- that implement the technique, and a
- 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,
- It is slow because of many calls to the math library.
- It can have numerical stability problems when x1 is very close to zero.
These are serious problems if you are doing
- 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
- 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 Erlang, exponential,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
- Carter, E.F, 1994; The Generation and Application of Random Numbers, Forth Dimensions Vol XVI Nos 1 & 2, Forth Interest Group, Oakland California
- Knuth, D.E., 1981; The Art of Computer Programming, Volume 2 Seminumerical Algorithms, Addison-Wesley, Reading Mass., 688 pages, ISBN 0-201-03822-6
- MacDougall,M.H., 1987; Simulating Computer Systems, M.I.T. Press, Cambridge, Ma., 292 pages, ISBN 0-262-13229-X
- 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
- Rubinstein, R.Y., 1981; Simulation and the Monte Carlo method, John Wiley & Sons, ISBN 0-471-08917-6
See Also
- : A
- 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/
Generating Gaussian Random Numbers(转)的更多相关文章
- C++ Standard-Library Random Numbers
Extracted from Section 17.4 Random Numbers, C++ Primer 5th. Ed. The random-number library generates ...
- Random Numbers Gym - 101466K dfs序+线段树
Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...
- 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 ...
- [Python] Generating random numbers using numpy lib
import numpy as np def test_run(): data=np.random.random((3,4)) """ [[ 0.80150549 0.9 ...
- Random numbers
Most computer programs do the same thing every time they execute, given the same inputs, so they are ...
- [Swift] 随机数 | Random numbers
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [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 ...
- 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 ...
- K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)
题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...
随机推荐
- java.lang.IllegalArgumentException: SessionContext must be an HTTP compatible implementation.:模块化本地测试shiro的一些总结
项目由于是多模块的,所以,测试的时候我想现将shiro框架进行本地测试,然后再放入框架里面,但是这个困扰我了两天了都,其实我应该想到的,只是想多试试,最后还不如多想想 先说一下系统的基本情况,项目是多 ...
- 第一百五十四节,封装库--JavaScript,表单验证--提交验证
封装库--JavaScript,表单验证--提交验证 将表单的所有必填项,做一个判断函数,填写正确时返回布尔值 最后在提交时,判断每一项是否正确,全部正确才可以 提交 html <div id= ...
- 日期类Date
Java在日期类中封装了有关日期和时间的信息,用户可以通过调用相应的方法来获取系统时间或设置日期和时间.Date类中有很多方法在JDK1.0公布后已经过时了,在8.3中我们将介绍JDK1.0中新加的用 ...
- Matrix_tree Theorem 矩阵树定理学习笔记
Matrix_tree Theorem: 给定一个无向图, 定义矩阵A A[i][j] = - (<i, j>之间的边数) A[i][i] = 点i的度数 其生成树的个数等于 A的任意n ...
- MATLAB中的文件类型总结
% ***.m文件 : 脚本文件或者函数文件或者:% ***.mat文件:数据存储文件(二进制文件,可以ASCII码形式保存和加载,% 类似于单行EXCEL表格) ...
- openwrt U盘启动
参考链接: http://m.blog.csdn.net/blog/zcynical/44892785
- 【Ubuntu】Windows硬盘安装Ubuntu14.04
参考:http://diybbs.zol.com.cn/1/33925_1981.html http://www.cnblogs.com/allenjin/archive/2011/11/26/22 ...
- Unity中内嵌网页插件UniWebView使用总结
目前有三种方式可以实现在Unity工程中实现内嵌网页的功能: 1. UnityWebCore:只支持Windows平台,调用浏览器内核,将网页渲染到mesh,作为gameObject. 2. Un ...
- poj 3204(最小割)
题目链接:http://poj.org/problem?id=3204 思路:显然只有增大那最小割边集上的边才能增加最大流,因此,我们可以先跑一遍最大流,然后对于那些满足条件的边u->v,当且仅 ...
- 第七篇:两个经典的文件IO程序示例
前言 本文分析两个经典的C++文件IO程序,提炼出其中文件IO的基本套路,留待日后查阅. 程序功能 程序一打印用户指定的所有文本文件,程序二向用户指定的所有文本文件中写入数据. 程序一代码及其注释 # ...