diff --git a/src/frontend/inp.c b/src/frontend/inp.c index 38c23afef..af7e86e8f 100644 --- a/src/frontend/inp.c +++ b/src/frontend/inp.c @@ -340,6 +340,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 @@ -381,6 +427,8 @@ inp_spsource(FILE *fp, bool comfile, char *filename, bool intfile) if (fp || intfile) { deck = inp_readall(fp, dir_name, comfile, intfile, &expr_w_temper); + /* 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 fa22922b3..b2e28068f 100644 --- a/src/include/ngspice/fteext.h +++ b/src/include/ngspice/fteext.h @@ -221,6 +221,7 @@ extern char *find_back_assignment(const char *s, const char *start); extern struct line *line_nconc(struct line *head, struct line *rest); extern struct line *line_reverse(struct line *head); extern void mc_free(void); +void eval_seed_opt(struct line *deck); extern char **circarray; extern void rem_tlist(struct pt_temper *p);