1/* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 21 unchanged lines hidden (view full) --- 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#if defined(LIBC_SCCS) && !defined(lint) 35static char sccsid[] = "@(#)random.c 8.2 (Berkeley) 5/19/95"; 36#endif /* LIBC_SCCS and not lint */ 37#include <sys/cdefs.h> |
38__FBSDID("$FreeBSD: head/lib/libc/stdlib/random.c 110321 2003-02-04 11:24:08Z ache $"); |
39 40#include "namespace.h" 41#include <sys/time.h> /* for srandomdev() */ 42#include <fcntl.h> /* for srandomdev() */ 43#include <stdio.h> 44#include <stdlib.h> 45#include <unistd.h> /* for srandomdev() */ 46#include "un-namespace.h" --- 90 unchanged lines hidden (view full) --- 137#define SEP_4 1 138 139/* 140 * Array versions of the above information to make code run faster -- 141 * relies on fact that TYPE_i == i. 142 */ 143#define MAX_TYPES 5 /* max number of types above */ 144 |
145#define NSHUFF 100 /* to drop part of seed -> 1st value correlation */ 146 |
147static long degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; 148static long seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; 149 150/* 151 * Initially, everything is set up as if from: 152 * 153 * initstate(1, randtbl, 128); 154 * --- 106 unchanged lines hidden (view full) --- 261 * information a given number of times to get rid of any initial dependencies 262 * introduced by the L.C.R.N.G. Note that the initialization of randtbl[] 263 * for default usage relies on values produced by this routine. 264 */ 265void 266srandom(x) 267 unsigned long x; 268{ |
269 long i, lim; |
270 |
271 state[0] = x; |
272 if (rand_type == TYPE_0) |
273 lim = NSHUFF; |
274 else { |
275 for (i = 1; i < rand_deg; i++) 276 state[i] = good_rand(state[i - 1]); 277 fptr = &state[rand_sep]; 278 rptr = &state[0]; |
279 lim = 10 * rand_deg; |
280 } |
281 for (i = 0; i < lim; i++) 282 (void)random(); |
283} 284 285/* 286 * srandomdev: 287 * 288 * Many programs choose the seed value in a totally predictable manner. 289 * This often causes problems. We seed the generator using the much more 290 * secure random(4) interface. Note that this particular seeding --- 211 unchanged lines hidden --- |