1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Erlangでの一様正規分布リストの生成

Last updated at Posted at 2015-05-16

概要

モンテカルロシミュレーションを行う際に必要となる平均0,標準偏差1の一様正規分布乱数をN個並べたリストをErlangで生成する関数のメモ。

Source

ndist.erl
-module(ndist).
-export([gen/1]).

%% Box-Muller変換関数
%% とりあえず簡易的にcosのみ。cosをsinに変えたものを使って2系統の生成が可能。
bm(X,Y) when is_number(X),is_number(Y) ->
  math:sqrt(-2.0 * math:log(X)) * math:cos(2 * math:pi() * Y).

%% 必要な数の要素を作成
seq(N) when is_integer(N) ->
  [A * random:uniform() || A <- lists:duplicate(N,1)].

%% 乱数生成
gen(N) when is_integer(N) ->
  random:seed(now()),
  [bm(X,Y) || {X,Y} <- lists:zip(seq(N),seq(N))];
gen(_N) ->
  false.

使い方

$ erl
> c(ndist).                 # => {ok,ndist}
> ndist:gen(100).           # => [0.12345,0.3214,-2.134...]
> s * (ndist:gen(100) + m)  # => 平均値m、標準偏差sの一様正規分布乱数を生成する場合
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?