2013-02-16 122 views
-1

給定一個生成1到5之間的隨機數的函數F,我將如何使用F編寫一個函數G來生成1和7之間的隨機數?給定一個隨機數生成器,用它來創建一個新的生成器

G生成的每個數字的概率應該是相同的。

到目前爲止,我已經嘗試過G=F + (5*(F/7))這個公式,儘管我不確定所有的概率是否相同。

+0

,你的問題是什麼? – 2013-02-16 03:06:40

+0

想知道它是否正確的解決方案...猜它不是...以上是不等概率...發現這... http://stackoverflow.com/questions/137783/expand-a-random-range - 從15到17?RQ = 1 – Lav 2013-02-16 03:26:58

回答

0

僞代碼:

function g() 
    repeat 
     // result will be equidistributed in 0..24 
     result = (f()-1)*5 + (f()-1); 
     // wait till we have something below 21, on average this happens 21 out of 25 times in the first pass 
     if (result < 21): return 1 + (result % 7)