2 * sample_many - generate many random values via 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_many.c,v 30.1 2007/03/16 11:09:46 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/RCS/sample_many.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 * many_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 */
48 #define RESEED 1000 /* number of random numbers to generate */
49 #define MANY 100 /* number of random numbers to generate */
51 extern char *program
; /* our name */
55 main(int argc
, char **argv
)
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 */
73 seed
= convstr2z(argv
[2]);
74 cnt
= strtol(argv
[1], NULL
, 0);
77 seed
= _zero_
; /* use the default seed */
78 cnt
= strtol(argv
[1], NULL
, 0);
81 seed
= _zero_
; /* use the default seed */
85 fprintf(stderr
, "usage: %s [[bits] seed_string]\n", program
);
89 fprintf(stderr
, "%s: cnt:%d must be > 0\n", program
, (int)cnt
);
96 libcalc_call_me_first();
99 * reseed every so often
101 for (j
=0; j
< RESEED
; ++j
) {
106 prev_state
= zsrandom2(seed
, zconst
[1]);
107 if (prev_state
== NULL
) {
108 math_error("previous random state is NULL");
111 randomfree(prev_state
);
114 * generate random values forever
116 for (i
=0; i
< MANY
; ++i
) {
119 * generate random bits
121 zrandom(cnt
, &random_val
);
124 * convert into hex string
126 hexstr
= convz2hex(random_val
);
127 printf("%s\n", hexstr
);
139 * increment the seed to better test different seeds
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.
146 * Don't do this in real life applications!
148 * We want to add at least 2^32 to the seed, so
149 * we do the effect of:
151 * seed += ((last_val<<32) + last_val);
153 zshift(random_val
, 32, &tmp
);
154 zadd(tmp
, random_val
, &tmp2
);
157 zadd(seed
, tmp2
, &tmp
);
166 libcalc_call_me_last();