inp.c, enable .option seed=val || random || 'random'

setting the seed value of the random number generator either to val
or to a random number drawn from the amount
of seconds passed since 1.1.1970
May be overrideden at any time later by command 'setseed newval'
This commit is contained in:
h_vogt 2016-08-06 19:56:23 +02:00 committed by rlar
parent 06ca10911b
commit d8a6e11d56
2 changed files with 49 additions and 0 deletions

View File

@ -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;

View File

@ -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;