diff --git a/src/frontend/inp.c b/src/frontend/inp.c index 762849169..2f53fc644 100644 --- a/src/frontend/inp.c +++ b/src/frontend/inp.c @@ -304,6 +304,52 @@ mc_free(void) } +/* check for .option seed=[val|random] and set the random number generator */ +void +eval_seed_opt(struct line *deck) +{ + struct line *card; + bool has_seed = FALSE; + + for (card = deck; card; card = card->li_next) { + char *line = card->li_line; + if (*line == '*') + continue; + if (ciprefix(".option", line) || ciprefix("option", line)) { + char *begtok = strstr(line, "seed="); + if (begtok) + begtok = &begtok[5]; /*skip seed=*/ + if (begtok) { + if (has_seed) + fprintf(cp_err, "Warning: Multiple 'option seed=val|random' found!\n"); + char *token = gettok(&begtok); + /* option seed=random */ + if (eq(token, "random") || eq(token, "{random}")) { + time_t acttime = time(NULL); + /* get random value from time in seconds since 1.1.1970 */ + int rseed = (int)(acttime - 1470000000); + cp_vset("rndseed", CP_NUM, &rseed); + com_sseed(NULL); + has_seed = TRUE; + } + /* option seed=val*/ + else { + int sr = atoi(token); + if (sr <= 0) + fprintf(cp_err, "Warning: Cannot convert 'option seed=%s' to seed value, skipped!\n", token); + else { + cp_vset("rndseed", CP_NUM, &sr); + com_sseed(NULL); + has_seed = TRUE; + } + } + tfree(token); + } + } + } +} + + /* The routine to source a spice input deck. We read the deck in, take * out the front-end commands, and create a CKT structure. Also we * filter out the following cards: .save, .width, .four, .print, and @@ -344,6 +390,8 @@ inp_spsource(FILE *fp, bool comfile, char *filename, bool intfile) if (fp || intfile) { deck = inp_readall(fp, dir_name, comfile, intfile); + /* here we check for .option seed=[val|random] and set the random number generator */ + eval_seed_opt(deck); /* files starting with *ng_script are user supplied command files */ if (deck && ciprefix("*ng_script", deck->li_line)) comfile = TRUE; diff --git a/src/include/ngspice/fteext.h b/src/include/ngspice/fteext.h index 4b38f03d1..f271f622f 100644 --- a/src/include/ngspice/fteext.h +++ b/src/include/ngspice/fteext.h @@ -215,6 +215,7 @@ extern struct line *inp_readall(FILE *fp, char *dir_name, bool comfile, bool int extern FILE *inp_pathopen(char *name, char *mode); extern char *search_identifier(char *str, const char *identifier, char *str_begin); extern void mc_free(void); +void eval_seed_opt(struct line *deck); extern char** circarray;