Dim mu As Integer 2 15 Dim x

  • Slides: 16
Download presentation

擬似乱数列の発生:プログラム例 Dim mu As Integer = 2 ^ 15 Dim x, lambda, c As

擬似乱数列の発生:プログラム例 Dim mu As Integer = 2 ^ 15 Dim x, lambda, c As Integer Console. Write. Line("xの初期値(0 -32768)を入力して下さい") Console. Write("x 0=") x = Console. Read. Line() lambda = 12869 c = 6925 For i As Integer = 1 To 20 x = (lambda * x + c) Mod mu Console. Write. Line("{0, 8}", x) Next

サイコロの出目の発生:プログラム例 Dim mu As Integer = 2 ^ 15 Sub Main() Console. Title =

サイコロの出目の発生:プログラム例 Dim mu As Integer = 2 ^ 15 Sub Main() Console. Title = "乱数の発生" Dim x, x_dice As Integer, n As Integer = 100 Console. Write. Line("xの初期値(0 -32768)を入力して下さい") Console. Write("x 0=") 一様乱数を生成する関数 x = Console. Read. Line() For i As Integer = 1 To n x_dice = Fix(6 * uni_rnd(x) / mu + 1) If i Mod 10 <> 0 Then 1≦x< 7の Console. Write("{0, 5}", x_dice) Else 実数列を生 Console. Write. Line("{0, 5}", x_dice) 成 End If x = uni_rnd(x) Next xを次の値に置き直す End Sub

サイコロの出目の発生:プログラム例(つづき) Function uni_rnd(x) Dim lambda, c As Integer lambda = 12869 c = 6925

サイコロの出目の発生:プログラム例(つづき) Function uni_rnd(x) Dim lambda, c As Integer lambda = 12869 c = 6925 x = (lambda * x + c) Mod mu Return x End Function 一様乱数を計算する 関数副プログラム

サイコロの出目の頻度:プログラム例 Dim mu As Integer = 2 ^ 15 各目の Sub Main() カウンター Console.

サイコロの出目の頻度:プログラム例 Dim mu As Integer = 2 ^ 15 各目の Sub Main() カウンター Console. Title = "乱数の発生" Dim x, x_dice, n, c 1, c 2, c 3, c 4, c 5, c 6 As Integer 各目の Dim p 1, p 2, p 3, p 4, p 5, p 6 As Single 頻度 c 1 = 0 : c 2 = 0 : c 3 = 0 : c 4 = 0 : c 5 = 0 : c 6 = 0 Console. Write. Line("xの初期値(0 -32768)を入力して下さい") Console. Write("x 0=") x = Console. Read. Line() n = 1000000 試行回数

サイコロの出目の頻度:プログラム例(つづき) For i As Integer = 1 To n x_dice = Fix(6 * uni_rnd(x)

サイコロの出目の頻度:プログラム例(つづき) For i As Integer = 1 To n x_dice = Fix(6 * uni_rnd(x) / mu + 1) Select Case x_dice Case 1 c 1 = c 1 + 1 出目の判定 Case 2 とカウント c 2 = c 2 + 1 Case 3 c 3 = c 3 + 1 Case 4 c 4 = c 4 + 1 Case 5 c 5 = c 5 + 1 Case 6 c 6 = c 6 + 1 End Select x = uni_rnd(x) Next

サイコロの出目の頻度:プログラム例(つづき) p 1 = c 1 / n Console. Write. Line("1の確率は{0}", p 2 =

サイコロの出目の頻度:プログラム例(つづき) p 1 = c 1 / n Console. Write. Line("1の確率は{0}", p 2 = c 2 / n Console. Write. Line("2の確率は{0}", p 3 = c 3 / n Console. Write. Line("3の確率は{0}", p 4 = c 4 / n Console. Write. Line("4の確率は{0}", p 5 = c 5 / n Console. Write. Line("5の確率は{0}", p 6 = c 6 / n Console. Write. Line("6の確率は{0}", End Sub p 1) p 2) p 3) p 4) p 5) p 6) 出目の頻度 と表示

サイコロの出目の頻度:プログラム例(つづき) Function uni_rnd(x) Dim lambda, c As Integer lambda = 12869 c = 6925

サイコロの出目の頻度:プログラム例(つづき) Function uni_rnd(x) Dim lambda, c As Integer lambda = 12869 c = 6925 x = (lambda * x + c) Mod mu Return x End Function 一様乱数を計算する 関数副プログラム