File: random.c

package info (click to toggle)
irsim 9.7.104-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,964 kB
  • sloc: ansic: 24,763; sh: 7,499; makefile: 418; csh: 269; tcl: 88
file content (425 lines) | stat: -rw-r--r-- 10,032 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*--------------------------------------------------------------*/
/* random ---							*/
/*								*/
/*	Loadable package to support pseudorandom manipulations	*/
/* 	in Tcl.  Based on the C stdlib "rand48" routines (48-	*/
/*	bit integer arithmetic).				*/
/*								*/
/* No known copyright --- this source code is apparently	*/
/* freeware.							*/
/*								*/
/* Integrated into the Tcl-based IRSIM simulator March 2003,	*/
/* R. Timothy Edwards, Open Circuit Design, Inc., for MultiGiG	*/
/* Ltd.								*/
/*--------------------------------------------------------------*/

/*
 *---------------------------------------------------------------
 * Return a pseudorandom number.			
 *
 *   Usage:   random [option]
 *
 *   By default (no option), the result is a floating-point value
 *   taken uniformly from the range [0..1)
 *
 *   Options:
 *
 *   -reset
 *	Reseed the generator using a combination of current pid
 *	and current time.
 *
 *   -seed n
 *	Reseed the generator with the integer value n.
 *
 *   -integer ...
 *	Round the value returned down to the largest integer less
 *	than or equal to the number which would otherwise be
 *	returned.
 *
 *   -bitstream n
 *	Return a uniformly-distributed random bitstream length n.
 *
 *   -normal m s
 *	Return a number taken from a gaussian distrubution with
 *	mean m and standard deviation s.
 *
 *   -exponential m
 *	Return a number taken from an exponential distribution
 *	with mean m.
 *
 *   -uniform low high
 *	Return a number taken from uniform distribution on
 *	[low,high).
 *
 *   -chi2 n
 *	Return a number taken from chi2 distribution with n
 *	degrees of freedom.
 *
 *   -select n list
 *	Select n elements at random from the list "list", with
 *	replacement.
 *
 *   -choose n list
 *	Select n elements at random from the list "list", without
 *	replacement.
 *
 *   -permutation n
 *	If n is a number, return a permutation of 0..n - 1.
 *	If n is a list, return a permutation of its elements.
 *
 *---------------------------------------------------------------
 */
   
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include <setjmp.h>
#include <sys/types.h>
#include <sys/time.h>

#include "../base/loctypes.h"

#ifdef TCL_IRSIM

#include <tcl.h>

/*--------------------------------------------------------------*/
/* return a sample from a unit normal distribution 		*/
/*--------------------------------------------------------------*/

double rand_gauss_dev()
{
    static int iset=0;
    static double gset;
    double fac, r, v1, v2;
    double drand48();

    if  (iset == 0) {
	/* sample from a circular disk by using the rejection method */
	do {
	    v1=2.0*drand48()-1.0;
	    v2=2.0*drand48()-1.0;
	    r=v1*v1+v2*v2;
	} while (r >= 1.0);

	/* now generate a radial variation */
	fac=sqrt(-2.0*log(r)/r);

	/* store the next return value */
	gset=v1*fac;
	iset=1;

	/* and return the current value */
	return v2*fac;
    } else {
	/* now return the old stored value */
	iset=0;
	return gset;
    }
}

/*--------------------------------------------------------------*/
/* Return a sample from a Chi^2 distribution			*/
/*--------------------------------------------------------------*/

double rand_chi2_dev(int dof)
{
    int i;
    double r;
    double drand48();

    /* summing dof/2 exponentially distributed deviates costs less than
       summing dof squared normal deviates both in terms of number of
       random bits needed and in terms of number of log calls needed */

    r = 0;
    for (i=1;i<=dof/2;i++) {
	r += log(1-drand48());
    }
    r = -2 * r;

    /* now if there was an extra, we have to add in a squared normal */

    if (dof & 1) {
	double x;
	x = rand_gauss_dev();
	r += x*x;
    }
    return r;
}

/*--------------------------------------------------------------*/
/* Main Tcl command callback function for Tcl command "random"	*/
/*--------------------------------------------------------------*/

int do_random(ClientData cl, Tcl_Interp *interp, int argc, char *argv[])
{
    int i, j;

    char r[TCL_DOUBLE_SPACE];
    pid_t getpid();
    double drand48();
    long lrand48();
    void srand48();

    if (argc == 1) {
	Tcl_PrintDouble(interp, drand48(), r);
	Tcl_SetResult(interp, r, TCL_VOLATILE);
    }
    else if (strcmp(argv[1], "-integer") == 0) {
	int a, b;
	a = 0x80000000;	/* Minimum (maximum negative) */
	b = 0x7fffffff;	/* Maximum */
	if (argc == 3) {
	    if (Tcl_GetInt(interp, argv[2], &b) != TCL_OK)
		return TCL_ERROR;
	    a = -b;
	}
	else if (argc == 4) {
	    if (Tcl_GetInt(interp, argv[2], &a) != TCL_OK)
		return TCL_ERROR;
	    if (Tcl_GetInt(interp, argv[3], &b) != TCL_OK)
		return TCL_ERROR;
	}
	sprintf(r, "%.0f", a + (b-a)*drand48());
	Tcl_SetResult(interp, r, TCL_VOLATILE);
    }

    else if (strncmp(argv[1], "-bit", 4) == 0) {
	int a;
	long x;
	char *rstr;
	a = 32;
	if (argc == 3) {
	    if (Tcl_GetInt(interp, argv[2], &a) != TCL_OK)
		return TCL_ERROR;
	}
	rstr = malloc(a + 1);
	for (i = 0; i < a; i += 32) {
	   x = lrand48(); 
	   for (j = 0; j < 32 && (i + j < a); j++) {
	      rstr[i + j] = (x & 0x1) ? '1' : '0';
	      x >>= 1;
	   }
	}
	rstr[a] = '\0';
	Tcl_SetResult(interp, rstr, TCL_VOLATILE);
	free(rstr);
    }

    else if (strcmp(argv[1], "-reset") == 0) {
	srand48(time(0) + getpid());
	r[0] = 0;
	Tcl_SetResult(interp, r, TCL_VOLATILE);
    }
    else if (strcmp(argv[1], "-seed") == 0) {
	int seed = 12345;;
	if (argc == 3) {
	    if (Tcl_GetInt(interp, argv[2], &seed) != TCL_OK) {
		return TCL_ERROR;
	    }
	}
	srand48((long int)seed);
	r[0] = 0;
	Tcl_SetResult(interp, r, TCL_VOLATILE);
    }
    else if (strcmp(argv[1], "-normal") == 0) {
	double m, s;
	m = s = 1;
	if (argc == 3 || argc == 4) {
	    if (Tcl_GetDouble(interp, argv[2], &m) != TCL_OK) {
		return TCL_ERROR;
	    }
	}
	if (argc == 4) {
	    if (Tcl_GetDouble(interp, argv[3], &s) != TCL_OK) {
		return TCL_ERROR;
	    }
	}
	Tcl_PrintDouble(interp, m + s*rand_gauss_dev(m, s), r);
	Tcl_SetResult(interp, r, TCL_VOLATILE);
    }
    else if (strcmp(argv[1], "-exponential") == 0) {
	double m;
	m = 1;
	if (argc > 2) {
	    if (Tcl_GetDouble(interp, argv[2], &m) != TCL_OK) {
		return TCL_ERROR;
	    }
	}
	Tcl_PrintDouble(interp, -m*log(1-drand48()), r);
	Tcl_SetResult(interp, r, TCL_VOLATILE);
    }
    else if (strcmp(argv[1], "-uniform") == 0) {
	double a, b;

	a = 0;
	b = 1;
	if (argc > 2) {
	    if (Tcl_GetDouble(interp, argv[2], &a) != TCL_OK) {
		return TCL_ERROR;
	    }
	}
	if (argc > 3) {
	    if (Tcl_GetDouble(interp, argv[3], &b) != TCL_OK) {
		return TCL_ERROR;
	    }
	}
	Tcl_PrintDouble(interp, a + (b-a)*drand48(), r);
	Tcl_SetResult(interp, r, TCL_VOLATILE);
    }
    else if (strcmp(argv[1], "-chi2") == 0) {
	double dof;

	dof = 1;
	if (argc > 2) {
	    if (Tcl_GetDouble(interp, argv[2], &dof) != TCL_OK) {
		return TCL_ERROR;
	    }
	}
	Tcl_PrintDouble(interp, rand_chi2_dev(dof), r);
	Tcl_SetResult(interp, r, TCL_VOLATILE);
    }
    else if (strcmp(argv[1], "-select") == 0) {
	int n;
	if (argc != 4) {
	    Tcl_SetResult(interp, "must have a count and a list for random -select",
			  TCL_STATIC);
	    return TCL_ERROR;
	}
	if (Tcl_GetInt(interp, argv[2], &n) != TCL_OK) {
	    return TCL_ERROR;
	}
	else {
	    int list_count;
	    char **list;

	    if (Tcl_SplitList(interp, argv[3], &list_count,
			(CONST char ***)&list) != TCL_OK) {
		return TCL_ERROR;
	    }
	    for (i=0;i<n;i++) {
		Tcl_AppendElement(interp, list[(int) (list_count*drand48())]);
	    }
	    free(list);
	}
    }
    else if (strcmp(argv[1], "-permute") == 0) {
	int list_count;
	char *t;
	char **list;

	if (argc != 3) {
	    Tcl_SetResult(interp, "must have a list for random -permute",
			  TCL_STATIC);
	    return TCL_ERROR;
	}

	if (Tcl_SplitList(interp, argv[2], &list_count,
			(CONST char ***)&list) != TCL_OK) {
	    return TCL_ERROR;
	}
	for (i=1;i<list_count;i++) {
	    j = (i+1) * drand48();
	    if (i != j) {
		t = list[j];
		list[j] = list[i];
		list[i] = t;
	    }
	}
	for (i=0;i<list_count;i++) {
	    Tcl_AppendElement(interp, list[i]);
	}
	free(list);
    }
    else if (strcmp(argv[1], "-permutation") == 0) {
	int n;

	if (argc != 3) {
	    Tcl_SetResult(interp, "must have a count for random -permutation",
			  TCL_STATIC);
	    return TCL_ERROR;
	}

	if (Tcl_GetInt(interp, argv[2], &n) != TCL_OK) {
	    return TCL_ERROR;
	}
	else {
	    int j, t, *p;
	    p = (int *)ckalloc(n * sizeof(p[0]));
	    for (i=0;i<n;i++) {
		p[i] = i;
	    }
	    for (i=1;i<n;i++) {
		j = (i+1) * drand48();
		if (i != j) {
		    t = p[j];
		    p[j] = p[i];
		    p[i] = t;
		}
	    }
	    for (i=0;i<n;i++) {
		sprintf(r, "%d", p[i]);
		Tcl_AppendElement(interp, r);
	    }
	    free(p);
	}
    }
    else if (strcmp(argv[1], "-choose") == 0) {
	int n;
	if (argc != 4) {
	    Tcl_SetResult(interp, "must have a count and a list for random -choose",
			  TCL_STATIC);
	    return TCL_ERROR;
	}
	if (Tcl_GetInt(interp, argv[2], &n) != TCL_OK) {
	    return TCL_ERROR;
	}
	else {
	    int list_count;
	    char *t;
	    char **list;

	    if (Tcl_SplitList(interp, argv[3], &list_count,
			(CONST char ***)&list) != TCL_OK) {
		return TCL_ERROR;
	    }
	    for (i=0;i<n;i++) {
		j = (n-i) * drand48() + i;
		if (i != j) {
		    t = list[j];
		    list[j] = list[i];
		    list[i] = t;
		}
	    }
	    for (i=0;i<n;i++) {
		Tcl_AppendElement(interp, list[i]);
	    }
	    free(list);
	}
    }
    else {
	Tcl_SetResult(interp, "bad arguments for random", TCL_STATIC);
	return TCL_ERROR;
    }
    return TCL_OK;
}

/*--------------------------------------------------------------*/
/* Tcl Package initialization function for "random"		*/
/*--------------------------------------------------------------*/

int Random_Init(Tcl_Interp *interp)
{
    Tcl_CreateCommand(interp, "random", (Tcl_CmdProc *)do_random, NULL, NULL);

    /* Seed the generator with something, or else the first value	*/
    /* produced will always be zero.					*/
    srand48((long int)1023);

    return TCL_OK;
}

#endif /* TCL_IRSIM */