modified: SpatialOmicsCoord.py
[GalaxyCodeBases.git] / c_cpp / etc / calc / sample_many.c
blob501d17add69a43786aad1b9c7bb933cef923482f
1 /*
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/
32 * usage:
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>
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 */
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 */
54 int
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 */
64 int i;
65 int j;
68 * parse args
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);
88 if (cnt <= 0) {
89 fprintf(stderr, "%s: cnt:%d must be > 0\n", program, (int)cnt);
90 exit(2);
94 * libcalc setup
96 libcalc_call_me_first();
99 * reseed every so often
101 for (j=0; j < RESEED; ++j) {
104 * seed the generator
106 prev_state = zsrandom2(seed, zconst[1]);
107 if (prev_state == NULL) {
108 math_error("previous random state is NULL");
109 /*NOTREACHED*/
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);
130 * free
132 if (i < MANY-1) {
133 zfree(random_val);
135 free(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);
155 zfree(random_val);
156 zfree(tmp);
157 zadd(seed, tmp2, &tmp);
158 zfree(tmp2);
159 zfree(seed);
160 seed = tmp;
164 * libcalc shutdown
166 libcalc_call_me_last();
169 * all done
171 /* exit(0); */
172 return 0;