summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormillert <>2012-03-21 12:36:49 +0000
committermillert <>2012-03-21 12:36:49 +0000
commitbb1c08f6beedc5a89ad7713c914f734e29c76d74 (patch)
treec1820ad7ac29d6bff160ba4d8031d30a2f8ca01a
parentd037dd6595ed75b194259764511aead1bb922a19 (diff)
downloadopenbsd-bb1c08f6beedc5a89ad7713c914f734e29c76d74.tar.gz
openbsd-bb1c08f6beedc5a89ad7713c914f734e29c76d74.tar.bz2
openbsd-bb1c08f6beedc5a89ad7713c914f734e29c76d74.zip
Fix a bug where random() always returns 0 when srandom() is seeded
with 0. Use 1 and not 0 as the first element of the state array, similar to what glibc does. OK nicm@
-rw-r--r--src/lib/libc/stdlib/random.c5
1 files changed, 3 insertions, 2 deletions
diff --git a/src/lib/libc/stdlib/random.c b/src/lib/libc/stdlib/random.c
index 48e892042b..5a9a7c3313 100644
--- a/src/lib/libc/stdlib/random.c
+++ b/src/lib/libc/stdlib/random.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: random.c,v 1.15 2005/11/30 07:51:02 otto Exp $ */ 1/* $OpenBSD: random.c,v 1.16 2012/03/21 12:36:49 millert Exp $ */
2/* 2/*
3 * Copyright (c) 1983 Regents of the University of California. 3 * Copyright (c) 1983 Regents of the University of California.
4 * All rights reserved. 4 * All rights reserved.
@@ -196,7 +196,8 @@ srandom(unsigned int x)
196 if (rand_type == TYPE_0) 196 if (rand_type == TYPE_0)
197 state[0] = x; 197 state[0] = x;
198 else { 198 else {
199 state[0] = x; 199 /* A seed of 0 would result in state[] always being zero. */
200 state[0] = x ? x : 1;
200 for (i = 1; i < rand_deg; i++) { 201 for (i = 1; i < rand_deg; i++) {
201 /* 202 /*
202 * Implement the following, without overflowing 31 bits: 203 * Implement the following, without overflowing 31 bits: