Search code examples
d

What is the syntax in D program which is the same of set.seed ( 1234)" in R


The "srand "is the incorrect command. This is the first time I use the D-languages. What is the code for D as set.seed in 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();
    }

Solution

  • It depends on just what library you're using. You can do rand and srand if you import core.stdc.stdlib; but the best way is probably to use std.random.

    Do you care about what specifically the seed is? If not, you can use the automatic one and just call some random function:

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

    or see it yoursef:

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

    If you do use your own Random object, be sure to pass it by ref to any function that uses it!

    The Random(unpredictableSeed) is similar to doing srand(time()) in other languages. You could also do Random(1234) to use a specific seed.

    These examples come from here: http://dlang.org/phobos/std_random.html