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/
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>
44 #include "have_const.h"
47 #define DEF_CNT 128 /* default number of bits to generate */
49 extern char *program
; /* our name */
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 */
67 seed
= convstr2z(argv
[2]);
68 cnt
= strtol(argv
[1], NULL
, 0);
71 seed
= _zero_
; /* use the default seed */
72 cnt
= strtol(argv
[1], NULL
, 0);
75 seed
= _zero_
; /* use the default seed */
79 fprintf(stderr
, "usage: %s [[bits] seed_string]\n", program
);
83 fprintf(stderr
, "%s: cnt:%d must be > 0\n", program
, (int)cnt
);
86 printf("seed= 0x%s\n", convz2hex(seed
));
91 libcalc_call_me_first();
96 prev_state
= zsrandom2(seed
, zconst
[10]);
97 if (prev_state
== NULL
) {
98 math_error("previous random state is NULL");
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
);