chicken-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-hackers] Patch to use better PRNG on BSD's


From: Timo Myyrä
Subject: [Chicken-hackers] Patch to use better PRNG on BSD's
Date: Tue, 10 Apr 2012 19:15:44 +0300

I noticed that chicken uses old rand / srand functions when better
alternatives are present.
Patch makes BSD's use arc4random instead of rand.

I'm not 100% sure this is correct so test, at least the Mac OS X part
as it doesn't have arc4random_uniform() function.
Builds and seems to work OK on OpenBSD when applied to 4.7.0 version.

Timo

diff --git a/chicken.h b/chicken.h
index 8b1d751..58da0b4 100644
--- a/chicken.h
+++ b/chicken.h
@@ -1128,8 +1128,18 @@ extern double trunc(double);
                                                  C_unfix(end1) -
C_unfix(start1) ), C_SCHEME_UNDEFINED)
 #define C_words(n)                      C_fix(C_bytestowords(C_unfix(n)))
 #define C_bytes(n)                      C_fix(C_wordstobytes(C_unfix(n)))
+#if defined (C_XXXBSD)
+#if defined (C_MACOSX)
+#define C_random_fixnum(n)              ((arc4random() % (RAND_MAX +
1.0)) * C_unfix(n))
+#define C_randomize(n)                  (srandom(C_unfix(n)),
C_SCHEME_UNDEFINED)
+#else
+#define C_random_fixnum(n)              (arc4random_uniform(C_unfix(n)))
+#define C_randomize(n)                  (srandom(C_unfix(n)),
C_SCHEME_UNDEFINED)
+#endif
+#else
 #define C_random_fixnum(n)
C_fix((C_word)(((double)rand())/(RAND_MAX + 1.0) * C_unfix(n)))
 #define C_randomize(n)                  (srand(C_unfix(n)), C_SCHEME_UNDEFINED)
+#endif
 #define C_block_size(x)                 C_fix(C_header_size(x))
 #define C_pointer_address(x)            ((C_byte *)C_block_item((x), 0))
 #define C_block_address(ptr, n, x)      C_a_unsigned_int_to_num(ptr, n, x)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]