3
2

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.

シェル芸で指数乱数を発生させる

Last updated at Posted at 2016-05-31

ワンライナーで指数乱数を発生させるシェル芸です。

ちょっとしたシミュレーションを"パッ"とやりたい時、ワンライナーでお手軽に乱数を発生させられます。

逆変換法ってのを使うと一様乱数から、任意の確率分布に従う乱数を得ることができるので、基本的に、一様乱数さえ作ることができれば、様々な種類の乱数が少ない手数で作れるんですね。

まずは一様乱数作成ワンライナー

cat /dev/random | nkf -W | tr -dc '0-9' | gfold -b10 | sed 's/^/0./g' | head -n 1000

上記のワンライナーで[0-1]の一様乱数が1000個作られます。

次に、指数乱数を作成。

cat /dev/random | nkf -W | tr -dc '0-9' | gfold -b10 | sed 's/^/0./g' | head -n 1000 | awk '{print -log($1)}'

これで、平均1の指数乱数が1000個作られます。

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?