1 /*
2  * sample_rand - test the libcalc 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  *	test_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 
49 extern char *program;	/* our name */
50 
51 
52 int
main(int argc,char ** argv)53 main(int argc, char **argv)
54 {
55 	RANDOM *prev_state;	/* previous random number state */
56 	ZVALUE seed;		/* seed for Blum-Blum-Shub */
57 	ZVALUE random_val;	/* random number produced */
58 	long cnt;		/* number of bits to generate */
59 	char *hexstr;		/* random number as hex string */
60 
61 	/*
62 	 * parse args
63 	 */
64 	program = argv[0];
65 	switch (argc) {
66 	case 3:
67 		seed = convstr2z(argv[2]);
68 		cnt = strtol(argv[1], NULL, 0);
69 		break;
70 	case 2:
71 		seed = _zero_;	/* use the default seed */
72 		cnt = strtol(argv[1], NULL, 0);
73 		break;
74 	case 1:
75 		seed = _zero_;	/* use the default seed */
76 		cnt = DEF_CNT;
77 		break;
78 	default:
79 		fprintf(stderr, "usage: %s [[bits] seed_string]\n", program);
80 		exit(1);
81 	}
82 	if (cnt <= 0) {
83 		fprintf(stderr, "%s: cnt:%d must be > 0\n", program, (int)cnt);
84 		exit(2);
85 	}
86 	printf("seed= 0x%s\n", convz2hex(seed));
87 
88 	/*
89 	 * libcalc setup
90 	 */
91 	libcalc_call_me_first();
92 
93 	/*
94 	 * seed the generator
95 	 */
96 	prev_state = zsrandom2(seed, _ten_);
97 	if (prev_state == NULL) {
98 		math_error("previous random state is NULL");
99 		/*NOTREACHED*/
100 	}
101 
102 	/*
103 	 * generate random bits
104 	 */
105 	zrandom(cnt, &random_val);
106 
107 	/*
108 	 * convert into hex string
109 	 */
110 	hexstr = convz2hex(random_val);
111 	printf("random= 0x%s\n", hexstr);
112 
113 	/*
114 	 * all done
115 	 */
116 	/* exit(0); */
117 	return 0;
118 }
119