Menu

#311 set rndseed does not work

v1.0 (example)
closed-invalid
None
5
2015-07-06
2015-07-06
No

The attached circuit does not work as expected.
A "set rndseed=33" in a control section does not have
the expected effect that multiple runs of the file all
give exactly the same output. Using the command in
spinit is OK, but less convenient.

-marcel

test rndseed
v1 1 0 DC 150 trnoise ( 1V 10m 0 0 )
.tran 0.01 0.4

.control
    set rndseed=33
    run
    plot -i(v1)
.endc

.end

Discussion

  • marcel hendrix

    marcel hendrix - 2015-07-06
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,17 +1,8 @@
     The attached circuit does not work as expected.
    -The "set rndseed=33" does not have the expected effect
    -that multiple runs of the file all give exactly the same
    -output.
    -
    -Hypothesis based on debugging:
    -Random numbers are pre-computed in large batches 
    -BEFORE the set command is processed?
    -
    -There is also a test in main.c for rnd_seed, far before
    -the circuit is parsed (and "set" is executed).
    -Probably this should be test for an rnd_seed in the
    -program environment, setup in the OS shell, not for a 
    -variable set inside the program.
    +A "set rndseed=33" in a control section does not have 
    +the expected effect that multiple runs of the file all 
    +give exactly the same output. Using the command in
    +spinit is OK, but less convenient.
    
     -marcel
    
     
  • marcel hendrix

    marcel hendrix - 2015-07-06

    Here is a possible fix that allows to set rndseed on the
    OS shell commandline before running ngspice. Setting rndseed
    in the spinit file should also be OK. Setting rndseed in
    a control section will not work.

    /* Check if a seed has been set by the command 'set rndseed=value'
       in spinit (or on the OS commandline) with integer value > 0. 
       If available, call srand(value).
       This will override the call to srand in main.c.
       Checkseed should be put in front of any call to rand or CombLCGTaus.. .
    */
    void checkseed(void) {
       int newseed;
       static int oldseed;
       static int gotit = 0, myseed = 0;
       if (gotit == 0) { 
            char *varseed = getenv("rndseed"); 
            if (varseed != NULL) myseed = (unsigned int)atoi(varseed); gotit = 1; }
       if (myseed > 0 && oldseed != myseed) { 
            srand((unsigned int)myseed); TausSeed(); oldseed = myseed; return; }
       if (cp_getvar("rndseed", CP_NUM, &newseed)) {
          if ((newseed > 0) && (oldseed != newseed)) { 
            srand((unsigned int)newseed); TausSeed(); oldseed = newseed; }
       }
    }
    
     
  • marcel hendrix

    marcel hendrix - 2015-07-06

    The observed behavior of rndseed is not a bug as
    it is clearly documented in the manual that rndseed
    should be changed in the spinit or .spiceinit file.
    Setting it in a .control section is not mentioned
    anywhere.

    That the patch causes rndseed to be (also) under
    control of the OS shell is a convenient new feature,
    not a bugfix.

    This bug will be closed.

     
  • marcel hendrix

    marcel hendrix - 2015-07-06
    • status: open --> closed-invalid
    • assigned_to: marcel hendrix
     
  • Robert Larice

    Robert Larice - 2015-07-06

    ~~~~~
    :::html
    Hello marcel,

    There is an obscure feature in the ngspice frontend
    which allows prefixing .control commands with "pre_"

    I forgot the exact syntax,
    but would you please try something like
    pre_set rndseed = 42

    Regards,
    Robert

     

Log in to post a comment.