> I just saw that ECL read /dev/urandom on startup,
> obviously to initialize its random number generator.
...
> * If the randomization at startup is wanted, how
> about reading much less
> (eg. 3 words) and using a pseudo-random-number
> generator to fill the random state?
...
> I'd be willing to provide a patch for the third point above - if there's a
> chance to
get it included.
Here it is; using some bytes of /dev/urandom it mixes more entropy into the RNG.
I noticed that fread() with current debian would always buffer 4kB, so I had to
move to
open()/read().
Regards,
Phil
diff --git src/c/num_rand.d src/c/num_rand.d
index 61caa31..3e6d76f 100644
--- src/c/num_rand.d
+++ src/c/num_rand.d
@@ -20,6 +20,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <ecl/internal.h>
+#include <fcntl.h>
+#if !defined(_MSC_VER) && !defined(__MINGW32__)
+# include <sys/stat.h>
+/* it isn't pulled in by fcntl.h */
+#endif
#if 0
@@ -66,22 +71,28 @@ init_random_state()
cl_index bytes = sizeof(ulong) * (MT_N + 1);
cl_object a = ecl_alloc_simple_base_string(bytes);
ulong *mt = (ulong*)a->base_string.self;
- int j;
+ int j = 0;
#if !defined(ECL_MS_WINDOWS_HOST)
- FILE *fp = fopen("/dev/urandom","r");
- if (fp) {
- fread(mt, sizeof(*mt), MT_N, fp);
- for (j=0; j < MT_N; j++){
- mt[j] &= 0xffffffffUL;
+ /* fopen() might read full 4kB blocks and discard
+ * a lot of entropy, so use open() */
+ int fh = open("/dev/urandom", O_RDONLY);
+ char buffer[16];
+ if (fh != -1) {
+ j = read(fh, buffer, sizeof(buffer));
+ for (; j < sizeof(buffer) && j < MT_N; j++){
+ mt[j] = buffer[j];
}
- fclose(fp);
- } else
+ close(fh);
+ }
#endif
{
/* cant get urandom, use crappy source */
- mt[0] = (rand() + time(0)) & 0xffffffffUL;
- for (j=1; j < MT_N; j++){
+ /* and/or fill rest of area */
+ mt[j++] = (rand() + time(0)) & 0xffffffffUL;
+ for (; j < MT_N; j++){
mt[j] = (1812433253UL * (mt[j-1] ^ (mt[j-1] >> 30)) + j);
+ if (j >= 16)
+ mt[j] ^= mt[j-16];
mt[j] &= 0xffffffffUL;
}
}
diff --git src/cmp/cmpglobals.lsp src/cmp/cmpglobals.lsp
index 1c98c60..6fb963e 100644
--- src/cmp/cmpglobals.lsp
+++ src/cmp/cmpglobals.lsp
@@ -259,7 +259,7 @@ lines are inserted, but the order is preserved")
(defvar *compile-in-constants* ; we either include constants as a C literal
#+ecl-min t ; or as an bound-in binary segment in the
- #-ecl-min nil) ; compiled file (si::{get,add}-cdata)
+ #-ecl-min t) ; compiled file (si::{get,add}-cdata)
(defvar *proclaim-fixed-args* nil) ; proclaim automatically functions
; with fixed number of arguments.
------------------------------------------------------------------------------
BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA
The must-attend event for mobile developers. Connect with experts.
Get tools for creating Super Apps. See the latest technologies.
Sessions, hands-on labs, demos & much more. Register early & save!
http://p.sf.net/sfu/rim-blackberry-1
_______________________________________________
Ecls-list mailing list
Ecls-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ecls-list