怎樣制造及編寫亂數器

2008-02-23 1:59 am
亂數在電腦中怎樣編寫及運作,有否微型亂數器買??及怎樣制造??

回答 (2)

2008-02-23 4:23 am
✔ 最佳答案
電腦是台依據步驟執行的機器, 根本沒法產生亂數, 它的亂數是 "偽隨機數", 即是是假的.

如果用簡單的例子來說明, "偽隨機數" 會有以下的做法:
(假設現在要產生一個 0 至 10 十一個可能性的隨機數)
1. 取一種子數 (Seed)
2. 代入公式 (Seed x 7) / 11, 計算其餘數. 該餘數為第一個隨機數
3. 把剛生產出來的隨機數當作 Seed, 再一次運算, 得到第二個隨機數
4. 重覆

例如 Seed = 2
第一個亂數便是 2 x 7 / 11 的餘數 = 3 (14 除 11 剩 3)
第二個亂數便是 3 x 7 / 11 的餘數 = 10 (21 除 11 剩 10)
第三個亂數便是 10 x 7 / 11 的餘數 = 4 (70 除 11 剩 4)
...

當然, 電腦內部的公式會更複雜, 但原理相同. 這個方法最大的問題是, 只要輸入相同的 Seed, 亂數出現的次序會完全相同. 一般以言, 程式員會利用電腦的時間 (例如秒或千份之一秒) 作為 Seed, 這樣亂數的數列便會比較接近真正的亂數.

一般的電腦語言已內置了亂數的生產函式, 所以不用程式員編寫, 只要提供 Seed 便可. 當然你亦可以根據以上的原則編寫自己的亂數器.

至於微型亂數器... 抱歉, 這不我是知識範疇. 不過我想用上述的運算, 很多簡單的集成電路也能處理, 因為這只牽涉到簡單的乘除和一個計時器.
2008-02-23 6:56 am
In computer, a random number generator is NOT truely random.
It is pseudo random.

The method is to pick a SEED (which is a number) as input.
Then, use a pre-defined algorithm to calculate the first output which is the first ranodm number. The second, third, fourth random number will then follow on similarly.

The disadvanteage of this method is that, if you input the [ same ] seed the next trial, you will get the [ same ] sequence of random numbers as last trial.

One quick-and-dirty solution is to use the [ current time ] of the day as SEED, because time always changes. The random() function in early Basic or Pascal allow you to choose the current time as SEED.

But, this [ current time ] SEED is not guaranteed to be the best solution.

One very famous bad example is Netscape 3 (?). Someone can predict the random number generation of Netscape and this is a secuirty flaw. For details, you can visit
http://www.unix.org.ua/orelly/networking/puis/ch23_08.htm

In short, the key is how to really really really [ randomly ] pick a SEED to generate the random number sequence.


收錄日期: 2021-04-13 15:37:11
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20080222000051KK02112

檢視 Wayback Machine 備份