modified: src1/input.c
[GalaxyCodeBases.git] / c_cpp / etc / calc / sample_rand.c
blobe614b2cef7a9415bd5348c11b33922ea6f3ca08d
1 /*
2 * sample_rand - test the libcalc random number generator
4 * Copyright (C) 1999-2007 Landon Curt Noll
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.
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.
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.
20 * @(#) $Revision: 30.1 $
21 * @(#) $Id: sample_rand.c,v 30.1 2007/03/16 11:09:46 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/RCS/sample_rand.c,v $
24 * Under source code control: 1997/04/19 22:46:49
25 * File existed as early as: 1997
27 * chongo <was here> /\oo/\ http://www.isthe.com/chongo/
28 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
32 * usage:
33 * test_random [[bits] seed_string]
35 * seed_string something for which we can seed (def: default seed)
36 * bits number of bits to generate
40 #include <sys/types.h>
41 #include <stdio.h>
42 #include "calc.h"
43 #include "zrandom.h"
44 #include "have_const.h"
45 #include "lib_util.h"
47 #define DEF_CNT 128 /* default number of bits to generate */
49 extern char *program; /* our name */
52 int
53 main(int argc, char **argv)
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 */
62 * parse args
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);
82 if (cnt <= 0) {
83 fprintf(stderr, "%s: cnt:%d must be > 0\n", program, (int)cnt);
84 exit(2);
86 printf("seed= 0x%s\n", convz2hex(seed));
89 * libcalc setup
91 libcalc_call_me_first();
94 * seed the generator
96 prev_state = zsrandom2(seed, zconst[10]);
97 if (prev_state == NULL) {
98 math_error("previous random state is NULL");
99 /*NOTREACHED*/
103 * generate random bits
105 zrandom(cnt, &random_val);
108 * convert into hex string
110 hexstr = convz2hex(random_val);
111 printf("random= 0x%s\n", hexstr);
114 * all done
116 /* exit(0); */
117 return 0;