2016-02-10 38 views
2

」srand「是不正確的命令,這是我第一次使用D語言。 d爲R.D程序中與set.seed(1234)相同的語法是什麼「R

void main() { 
//srand(1234); ????// 
randInit(); 
auto x = RMatrix(10,1);foreach(rep; 0..1) { 
printR(rep.robj); 
double init = 0.0; 
    foreach(ii; 0..100) { 
     init = 0.5*init + rnorm(); 
    } 
    x[0,0] = init; 
    foreach(ii; 1..x.rows) { 
     x[ii,0] = 0.8*x[ii-1,0] + rnorm(); 
    } 

回答

5

set.seed這取決於你使用,你可以做randsrand如果你import core.stdc.stdlib;但最好的辦法可能是使用std.random正是庫。

待辦事項你關心什麼種子是特別的?如果沒有,你可以使用自動的,然後調用一些隨機函數:

// Generate a uniformly-distributed integer in the range [0, 14] 
auto i = uniform(0, 15); 

或看到它yoursef:

Random gen = Random(unpredictableSeed); 
auto r = uniform(0.0L, 100.0L, gen); 

如果你使用自己的Random對象,一定要通過ref通過它來使用它的任何功能!

Random(unpredictableSeed)與其他語言的srand(time())類似。您也可以使用Random(1234)來使用特定的種子。

這些例子都來自這裏:http://dlang.org/phobos/std_random.html

+0

你能不能在技術上只是做'均勻(T1,T2)'不指定發電機?據我所知,然後'uniform(T1,T2)'將用默認的隨機生成器進行初始化,該生成器使用'unpredictableSeed'初始化' – Bauss

+0

是的,我首先從文檔中引用了該示例! :) –

+0

我的不好,我沒有仔細閱讀! :) – Bauss

相關問題