1. 程式人生 > 實用技巧 >tcl中指定隨機數種子

tcl中指定隨機數種子

rand()

從區間[0, 1)中均勻取樣的隨機數。

set a [expr rand()]

srand(arg)

arg必須是整數,用於重置隨機數生成器的種子。返回該種子的第一個隨機數。每個直譯器都有自己的種子。rand()和srand()函式在加密上不安全,不能用於生成一次性密碼或會話金鑰。對於蒙特卡羅模擬的使用,它們也可能還不夠強大。

The arg, which must be an integer, is used to reset the seed for the random number generator. Returns the first random number from that seed. Each interpreter has its own seed.

The rand() and srand() functions arenotcryptographically secure, and mustnotbe used to generate one-time passwords or session keys. They are also probably not strong enough for Monte-Carlo simulation usage.

expr srand(123)

set a [expr rand()]

參考資料:

https://stackoverflow.com/questions/60181104/is-there-a-way-to-generate-pseudo-random-numbers-with-a-seed-in-tcl-8-5

https://wiki.tcl-lang.org/page/rand

https://wiki.tcl-lang.org/page/srand