一様分布

##########################################
# 一様分布の計算
#      coded by Y.Suganuma
##########################################

##########################################
# 一様分布の計算(P(X = x), P(X < x))
#      x : データ
#      a,b : 区間
#      pr : P(X = x) 
#      return : P(X < x)
##########################################

def uniform(x, a, b, pr)

	f = 0.0

	if x < a
		pr[0] = 0.0
		f     = 0.0
	elsif x > b
		pr[0] = 0.0
		f     = 1.0
	else
		y     = 1.0 / (b - a)
		pr[0] = y
		f     = y * (x - a)
	end

	return f
end

print("下限(a)は? ")
a = Float(gets())
print("上限(b)は? ")
b = Float(gets())
print("目的とする結果は?\n")
print("     =0 : 確率の計算( P(X = x) 及び P(X < x) の値)\n")
print("     =1 : p%値( P(X > u) = 0.01p となるuの値) ")
sw = Integer(gets())
pr = Array.new(1)

if sw == 0
	print("グラフ出力?(=1: yes,  =0: no) ")
	sw = Integer(gets())
	if sw == 0
			# 密度関数と分布関数の値
		print("   データは? ")
		x = Float(gets())
		f = uniform(x, a, b, pr)
		print("P(X = " + String(x) + ") = " + String(pr[0]) + ",  P( X < " + String(x) + ") = " + String(f) + " (区間 [" + String(a) + ", " + String(b) + "])\n")
			# グラフ出力
	else
		print("   密度関数のファイル名は? ")
		file1 = gets().strip()
		print("   分布関数のファイル名は? ")
		file2 = gets().strip()
		print("   データの上限は? ")
		up = Integer(gets())
		print("   刻み幅は? ")
		h    = Float(gets())
		out1 = open(file1, "w")
		out2 = open(file2, "w")
		x     = a
		while x < b+0.5*h
			if x > b-0.5*h and x < b+0.5*h
				x = b
			end
			f = uniform(x, a, b, pr)
			out1.print(String(x) + " " + String(pr[0]) + "\n")
			out2.print(String(x) + " " + String(f) + "\n")
			x += h
		end
		out1.close()
		out2.close()
	end
			# %値
else
	print("%の値は? ")
	x = Float(gets())
	p = 0.01 * x
	f = b - p * (b - a)
	print(String(x) + "%値 = " + String(f) + " (区間 [" + String(a) + ", " + String(b) + "])")
end