【问题标题】:How to generate normally distributed random number如何生成正态分布的随机数
【发布时间】:2014-11-06 03:16:31
【问题描述】:

有人可以解释或指向一个页面,该页面解释了如何在 matlab 中仅使用误差函数、误差函数的倒数和 rand()(0 到 1 之间的均匀随机数生成器)创建正态分布的随机数?随机数不必限制在某个区间内。我在理解误差函数的概念和它的逆函数,以及它与创建正态分布的随机数的关系时遇到了问题

【问题讨论】:

标签: matlab random gaussian normal-distribution


【解决方案1】:

您需要应用名为inverse transform sampling 的方法,该方法包括以下内容。假设您要生成具有给定分布函数 F 的随机变量。如果你可以计算出反函数F-1,那么你可以通过应用F-1得到想要的随机变量支持在区间[0,1]上均匀分布的随机样本。

误差函数(Matlab 中的erf)几乎给出了正态随机变量的分布函数。它的反函数在 Matlab 中称为erfinv。使用rand 生成均匀分布的随机数。

有了这些成分,您应该能够完成任务。请试一试,然后看到将鼠标悬停在矩形上的代码:

N = 1e6; % number of samples
x = erfinv(2*rand(1,N)-1); % note factor 2, because of definition of erf
hist(x,31) % plot histogram to check it is (approximately) normal

【讨论】:

  • 鼠标悬停的巧妙技巧。有没有简单的方法,或者您必须手动输入>! 等?
  • @JohnColeman 谢谢!用手。最繁琐的部分是反引号和<br>'s 将代码分成几行
【解决方案2】:

来自 Mathworks 的 link 似乎给出了答案。

这是链接中的示例:

% First, initialize the random number generator to make the results in this
% example repeatable.
rng(0,'twister');

% Create a vector of 1000 random values drawn from a normal distribution
% with a mean of 500 and a standard deviation of 5.
a = 5;
b = 500;
y = a.*randn(1000,1) + b;

% Calculate the sample mean, standard deviation, and variance.
stats = [mean(y) std(y) var(y)]

% stats =
% 
%   499.8368    4.9948   24.9483
%
% The mean and variance are not 500 and 25 exactly because they are
% calculated from a sampling of the distribution.

【讨论】:

    猜你喜欢
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 2018-06-22
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多