[討論] 不公正骰子 寫法已回收

看板MATLAB作者 (fonlintw0621)時間13年前 (2010/12/20 19:40), 編輯推噓1(103)
留言4則, 2人參與, 最新討論串1/2 (看更多)
程式需求 : a. 輸入出現六的機率 b. 剩餘數字出現機率均勻分配 clear all;clc; r = input('輸入不公正骰子點數6的機率'); n = r/100 + ([1-r/100] /5) .* [0 1 2 3 4 5]; for i = 1:100; if rand(1) >= 0 & rand(1) < n(1) computer(i) = 6 ; elseif rand(1) >= n(1) & rand(1) < n(2) computer(i) = 5 ; elseif rand(1) >= n(2) & rand(1) < n(3) computer(i) = 4 ; elseif rand(1) >= n(3) & rand(1) < n(4) computer(i) = 3 ; elseif rand(1) >= n(4) & rand(1) < n(5) computer(i) = 2 ; else computer(i) = 1 ; end end 請問哪裡有錯誤呢 ? 我發現 每一項 用 n = n + 1 跑 出現1~6 的項數,分配機率跟我預期的不太一樣。 -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.36.207.19

12/20 22:32, , 1F
亂數先一次產生ex. a = rand(1,1000)<-建議取樣數高1點
12/20 22:32, 1F

12/21 08:04, , 2F
你出老千~~~~XD
12/21 08:04, 2F

12/21 08:05, , 3F
咳恩(嚴肅狀) 你這樣rand(1)這麼多次 不就取亂數好幾次嗎?
12/21 08:05, 3F

12/21 08:16, , 4F
除了在for底下a=rand(1);一行之後就都用a來取代rand(1)
12/21 08:16, 4F
感謝樓上兩位 指點 ,點到重點了 XD ※ 編輯: fonlintw0621 來自: 140.118.198.52 (12/21 10:09)
文章代碼(AID): #1D3q2pAA (MATLAB)
文章代碼(AID): #1D3q2pAA (MATLAB)