]> granicus.if.org Git - vim/commitdiff
patch 8.1.2356: rand() does not use the best algorithm v8.1.2356
authorBram Moolenaar <Bram@vim.org>
Thu, 28 Nov 2019 21:13:14 +0000 (22:13 +0100)
committerBram Moolenaar <Bram@vim.org>
Thu, 28 Nov 2019 21:13:14 +0000 (22:13 +0100)
Problem:    rand() does not use the best algorithm.
Solution:   use xoshiro128** instead of xorshift. (Kaito Udagawa,
            closes #5279)

runtime/doc/eval.txt
src/evalfunc.c
src/testdir/test_random.vim
src/version.c

index b5460ffe3d4ea10943d4df10f4125f921b090472..e6f7283f2392089085505231fef02b30f782776f 100644 (file)
@@ -1,4 +1,4 @@
-*eval.txt*     For Vim version 8.1.  Last change: 2019 Nov 25
+*eval.txt*     For Vim version 8.1.  Last change: 2019 Nov 28
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -7641,7 +7641,7 @@ range({expr} [, {max} [, {stride}]])                              *range()*
 <
 
 rand([{expr}])                                         *rand()*
-               Return a pseudo-random Number generated with an xorshift
+               Return a pseudo-random Number generated with an xoshiro128**
                algorithm using seed {expr}.  The returned number is 32 bits,
                also on 64 bits systems, for consistency.
                {expr} can be initialized by |srand()| and will be updated by
@@ -9150,11 +9150,11 @@ sqrt({expr})                                            *sqrt()*
 srand([{expr}])                                                *srand()*
                Initialize seed used by |rand()|:
                - If {expr} is not given, seed values are initialized by
-                 time(NULL) a.k.a. epoch time.  This only has second
-                 accuracy.
-               - If {expr} is given, return seed values which x element is
-                 {expr}.  This is useful for testing or when a predictable
-                 sequence is expected.
+                 reading from /dev/urandom, if possible, or using time(NULL)
+                 a.k.a. epoch time otherwise; this only has second accuracy.
+               - If {expr} is given it must be a Number.  It is used to
+                 initialize the seed values.  This is useful for testing or
+                 when a predictable sequence is intended.
 
                Examples: >
                        :let seed = srand()
index c6e4679e54ba92c38a79fe5295d3524e3341f05e..6b00b4e6561189b712c19e452ff69120ca40f5d4 100644 (file)
@@ -5139,70 +5139,74 @@ f_pyxeval(typval_T *argvars, typval_T *rettv)
 f_rand(typval_T *argvars, typval_T *rettv)
 {
     list_T     *l = NULL;
-    UINT32_T   x, y, z, w, t;
-    static int rand_seed_initialized = FALSE;
-    static UINT32_T xyzw[4] = {123456789, 362436069, 521288629, 88675123};
-
-#define SHUFFLE_XORSHIFT128 \
-       t = x ^ (x << 11); \
-       x = y; y = z; z = w; \
-       w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
+    static list_T *globl = NULL;
+    UINT32_T   x, y, z, w, t, result;
+    listitem_T *lx, *ly, *lz, *lw;
 
     if (argvars[0].v_type == VAR_UNKNOWN)
     {
-       // When argument is not given, return random number initialized
-       // statically.
-       if (!rand_seed_initialized)
+       // When no argument is given use the global seed list.
+       if (globl == NULL)
        {
-           xyzw[0] = (varnumber_T)time(NULL);
-           rand_seed_initialized = TRUE;
+           // Initialize the global seed list.
+           f_srand(argvars, rettv);
+           l = rettv->vval.v_list;
+           if (l == NULL || list_len(l) != 4)
+           {
+               clear_tv(rettv);
+               goto theend;
+           }
+           globl = l;
        }
-
-       x = xyzw[0];
-       y = xyzw[1];
-       z = xyzw[2];
-       w = xyzw[3];
-       SHUFFLE_XORSHIFT128;
-       xyzw[0] = x;
-       xyzw[1] = y;
-       xyzw[2] = z;
-       xyzw[3] = w;
+       else
+           l = globl;
     }
     else if (argvars[0].v_type == VAR_LIST)
     {
-       listitem_T      *lx, *ly, *lz, *lw;
-
        l = argvars[0].vval.v_list;
-       if (list_len(l) != 4)
+       if (l == NULL || list_len(l) != 4)
            goto theend;
-
-       lx = list_find(l, 0L);
-       ly = list_find(l, 1L);
-       lz = list_find(l, 2L);
-       lw = list_find(l, 3L);
-       if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
-       if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
-       if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
-       if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
-       x = (UINT32_T)lx->li_tv.vval.v_number;
-       y = (UINT32_T)ly->li_tv.vval.v_number;
-       z = (UINT32_T)lz->li_tv.vval.v_number;
-       w = (UINT32_T)lw->li_tv.vval.v_number;
-       SHUFFLE_XORSHIFT128;
-       lx->li_tv.vval.v_number = (varnumber_T)x;
-       ly->li_tv.vval.v_number = (varnumber_T)y;
-       lz->li_tv.vval.v_number = (varnumber_T)z;
-       lw->li_tv.vval.v_number = (varnumber_T)w;
     }
     else
        goto theend;
 
+    lx = list_find(l, 0L);
+    ly = list_find(l, 1L);
+    lz = list_find(l, 2L);
+    lw = list_find(l, 3L);
+    if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
+    if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
+    if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
+    if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
+    x = (UINT32_T)lx->li_tv.vval.v_number;
+    y = (UINT32_T)ly->li_tv.vval.v_number;
+    z = (UINT32_T)lz->li_tv.vval.v_number;
+    w = (UINT32_T)lw->li_tv.vval.v_number;
+
+    // SHUFFLE_XOSHIRO128STARSTAR
+#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
+    result = ROTL(y * 5, 7) * 9;
+    t = y << 9;
+    z ^= x;
+    w ^= y;
+    y ^= z, x ^= w;
+    z ^= t;
+    w = ROTL(w, 11);
+#undef ROTL
+
+    lx->li_tv.vval.v_number = (varnumber_T)x;
+    ly->li_tv.vval.v_number = (varnumber_T)y;
+    lz->li_tv.vval.v_number = (varnumber_T)z;
+    lw->li_tv.vval.v_number = (varnumber_T)w;
+
     rettv->v_type = VAR_NUMBER;
-    rettv->vval.v_number = (varnumber_T)w;
+    rettv->vval.v_number = (varnumber_T)result;
     return;
 
 theend:
     semsg(_(e_invarg2), tv_get_string(&argvars[0]));
+    rettv->v_type = VAR_NUMBER;
+    rettv->vval.v_number = -1;
 }
 
 /*
@@ -7096,6 +7100,7 @@ f_sqrt(typval_T *argvars, typval_T *rettv)
 f_srand(typval_T *argvars, typval_T *rettv)
 {
     static int dev_urandom_state = -1;  // FAIL or OK once tried
+    UINT32_T x = 0, z;
 
     if (rettv_list_alloc(rettv) == FAIL)
        return;
@@ -7123,8 +7128,7 @@ f_srand(typval_T *argvars, typval_T *rettv)
                else
                {
                    dev_urandom_state = OK;
-                   list_append_number(rettv->vval.v_list,
-                                                (varnumber_T)buf.cont.number);
+                   x = buf.cont.number;
                }
                close(fd);
            }
@@ -7132,21 +7136,28 @@ f_srand(typval_T *argvars, typval_T *rettv)
        }
        if (dev_urandom_state != OK)
            // Reading /dev/urandom doesn't work, fall back to time().
-           list_append_number(rettv->vval.v_list, (varnumber_T)vim_time());
+           x = vim_time();
     }
     else
     {
        int         error = FALSE;
-       UINT32_T    x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
 
+       x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
        if (error)
            return;
-
-       list_append_number(rettv->vval.v_list, (varnumber_T)x);
     }
-    list_append_number(rettv->vval.v_list, 362436069);
-    list_append_number(rettv->vval.v_list, 521288629);
-    list_append_number(rettv->vval.v_list, 88675123);
+
+#define SPLITMIX32 ( \
+    z = (x += 0x9e3779b9), \
+    z = (z ^ (z >> 16)) * 0x85ebca6b, \
+    z = (z ^ (z >> 13)) * 0xc2b2ae35, \
+    z ^ (z >> 16) \
+    )
+
+    list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32);
+    list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32);
+    list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32);
+    list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32);
 }
 
 /*
index 9fe71a98c453e0b3b9d8c2f2372e97e566005cd8..f8ea407dc75cefec1dda8623359d7a2f38457666 100644 (file)
@@ -2,12 +2,12 @@
 
 func Test_Rand()
   let r = srand(123456789)
-  call assert_equal([123456789, 362436069, 521288629, 88675123], r)
-  call assert_equal(3701687786, rand(r))
-  call assert_equal(458299110, rand(r))
-  call assert_equal(2500872618, rand(r))
-  call assert_equal(3633119408, rand(r))
-  call assert_equal(516391518, rand(r))
+  call assert_equal([1573771921, 319883699, 2742014374, 1324369493], r)
+  call assert_equal(4284103975, rand(r))
+  call assert_equal(1001954530, rand(r))
+  call assert_equal(2701803082, rand(r))
+  call assert_equal(2658065534, rand(r))
+  call assert_equal(3104308804, rand(r))
 
   call test_settime(12341234)
   let s = srand()
index 4ca5c0f76c20adb2005d639f909e0851b002537d..7abb190fc78b8166ad4e3f600aa5f8268bb81c8a 100644 (file)
@@ -737,6 +737,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2356,
 /**/
     2355,
 /**/