#!/usr/koeki/bin/ruby # coding: euc-jp def fac(m) if m <= 1 1 else m * fac(m-1) end end def combi(n,m) fac(n) / fac(n-m) / fac(m) end STDERR.print("二項分布 B(n, p) \n") STDERR.print("起こる確率 p を決めて下さい: (0 < p < 1)\n") p = gets.to_f if p > 1 exit else q = 1 - p STDERR.print("標本数 n を決めて下さい。") n = gets.to_i open("bernouilli.dat", "w") do |file| for i in 0..n prob = combi(n,i) * p ** i * q ** (n-i) file.printf("%d\t%f\n", i, prob) end end end STDERR.print("このあと、gnuplot で plot \"bernoulli.dat\" を実行しましょう\n")