1 /*
2  * sample_many - generate many random values via random number generator
3  *
4  * Copyright (C) 1999-2007,2021  Landon Curt Noll
5  *
6  * Calc is open software; you can redistribute it and/or modify it under
7  * the terms of the version 2.1 of the GNU Lesser General Public License
8  * as published by the Free Software Foundation.
9  *
10  * Calc is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU Lesser General
13  * Public License for more details.
14  *
15  * A copy of version 2.1 of the GNU Lesser General Public License is
16  * distributed with calc under the filename COPYING-LGPL.  You should have
17  * received a copy with calc; if not, write to Free Software Foundation, Inc.
18  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  *
20  * Under source code control:	1997/04/19 22:46:49
21  * File existed as early as:	1997
22  *
23  * chongo <was here> /\oo/\	http://www.isthe.com/chongo/
24  * Share and enjoy!  :-)	http://www.isthe.com/chongo/tech/comp/calc/
25  */
26 
27 /*
28  * usage:
29  *	many_random [[bits] seed_string]
30  *
31  *	seed_string	something for which we can seed (def: default seed)
32  *	bits		number of bits to generate
33  */
34 
35 
36 #include <sys/types.h>
37 #include <stdio.h>
38 #include "calc.h"
39 #include "zrandom.h"
40 #include "have_const.h"
41 #include "lib_util.h"
42 
43 
44 #include "banned.h"	/* include after system header <> includes */
45 
46 
47 #define DEF_CNT 128	/* default number of bits to generate */
48 #define RESEED 1000	/* number of random numbers to generate */
49 #define MANY 100	/* number of random numbers to generate */
50 
51 extern char *program;	/* our name */
52 
53 
54 int
main(int argc,char ** argv)55 main(int argc, char **argv)
56 {
57 	RANDOM *prev_state;	/* previous random number state */
58 	ZVALUE seed;		/* seed for Blum-Blum-Shub */
59 	ZVALUE tmp;		/* temp value */
60 	ZVALUE tmp2;		/* temp value */
61 	ZVALUE random_val;	/* random number produced */
62 	long cnt;		/* number of bits to generate */
63 	char *hexstr;		/* random number as hex string */
64 	int i;
65 	int j;
66 
67 	/*
68 	 * parse args
69 	 */
70 	program = argv[0];
71 	switch (argc) {
72 	case 3:
73 		seed = convstr2z(argv[2]);
74 		cnt = strtol(argv[1], NULL, 0);
75 		break;
76 	case 2:
77 		seed = _zero_;	/* use the default seed */
78 		cnt = strtol(argv[1], NULL, 0);
79 		break;
80 	case 1:
81 		seed = _zero_;	/* use the default seed */
82 		cnt = DEF_CNT;
83 		break;
84 	default:
85 		fprintf(stderr, "usage: %s [[bits] seed_string]\n", program);
86 		exit(1);
87 	}
88 	if (cnt <= 0) {
89 		fprintf(stderr, "%s: cnt:%d must be > 0\n", program, (int)cnt);
90 		exit(2);
91 	}
92 
93 	/*
94 	 * libcalc setup
95 	 */
96 	libcalc_call_me_first();
97 
98 	/*
99 	 * reseed every so often
100 	 */
101 	for (j=0; j < RESEED; ++j) {
102 
103 		/*
104 		 * seed the generator
105 		 */
106 		prev_state = zsrandom2(seed, _one_);
107 		if (prev_state == NULL) {
108 			math_error("previous random state is NULL");
109 			/*NOTREACHED*/
110 		}
111 		randomfree(prev_state);
112 
113 		/*
114 		 * generate random values forever
115 		 */
116 		for (i=0; i < MANY; ++i) {
117 
118 			/*
119 			 * generate random bits
120 			 */
121 			zrandom(cnt, &random_val);
122 
123 			/*
124 			 * convert into hex string
125 			 */
126 			hexstr = convz2hex(random_val);
127 			printf("%s\n", hexstr);
128 
129 			/*
130 			 * free
131 			 */
132 			if (i < MANY-1) {
133 				zfree(random_val);
134 			}
135 			free(hexstr);
136 		}
137 
138 		/*
139 		 * increment the seed to better test different seeds
140 		 *
141 		 * NOTE: It is generally a bad idea to use the
142 		 *	 same random number generator to modify
143 		 *	 the seed.  We only do this below to
144 		 *	 try different seeds for debugging.
145 		 *
146 		 *	 Don't do this in real life applications!
147 		 *
148 		 * We want to add at least 2^32 to the seed, so
149 		 * we do the effect of:
150 		 *
151 		 *	seed += ((last_val<<32) + last_val);
152 		 */
153 		zshift(random_val, 32, &tmp);
154 		zadd(tmp, random_val, &tmp2);
155 		zfree(random_val);
156 		zfree(tmp);
157 		zadd(seed, tmp2, &tmp);
158 		zfree(tmp2);
159 		zfree(seed);
160 		seed = tmp;
161 	}
162 
163 	/*
164 	 * libcalc shutdown
165 	 */
166 	libcalc_call_me_last();
167 
168 	/*
169 	 * all done
170 	 */
171 	/* exit(0); */
172 	return 0;
173 }
174