From 22f0725aac300ed9b249f995df7889f6c203d1e0 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sun, 9 Jan 2022 22:48:29 +0000 Subject: vim-patch:8.1.2342: random number generator in Vim script is slow Problem: Random number generator in Vim script is slow. Solution: Add rand() and srand(). (Yasuhiro Matsumoto, closes vim/vim#1277) https://github.com/vim/vim/commit/06b0b4bc27077013e9b4b48fd1d9b33e543ccf99 Add missing method call usage to builtin.txt. vim_time and test_settime is N/A. Add a modeline to test_random.vim. Use typval_T* over listitem_T* vars so we don't need to use TV_LIST_ITEM_TV all over the place... Remove NULL list checks (tv_list_len covers this). --- runtime/doc/builtin.txt | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'runtime') diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index 4153c1c9d8..35beb97518 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -325,6 +325,7 @@ pumvisible() Number whether popup menu is visible pyeval({expr}) any evaluate |Python| expression py3eval({expr}) any evaluate |python3| expression pyxeval({expr}) any evaluate |python_x| expression +rand([{expr}]) Number get pseudo-random number range({expr} [, {max} [, {stride}]]) List items from {expr} to {max} readdir({dir} [, {expr}]) List file names in {dir} selected by {expr} @@ -441,6 +442,7 @@ spellsuggest({word} [, {max} [, {capital}]]) split({expr} [, {pat} [, {keepempty}]]) List make |List| from {pat} separated {expr} sqrt({expr}) Float square root of {expr} +srand([{expr}]) List get seed for |rand()| stdioopen({dict}) Number open stdio in a headless instance. stdpath({what}) String/List returns the standard path(s) for {what} str2float({expr} [, {quoted}]) Float convert String to Float @@ -5529,6 +5531,22 @@ range({expr} [, {max} [, {stride}]]) *range()* < Can also be used as a |method|: > GetExpr()->range() +< +rand([{expr}]) *rand()* + Return a pseudo-random Number generated with an xorshift + algorithm using seed {expr}. {expr} can be initialized by + |srand()| and will be updated by rand(). + If {expr} is omitted, an internal seed value is used and + updated. + + Examples: > + :echo rand() + :let seed = srand() + :echo rand(seed) + :echo rand(seed) +< + Can also be used as a |method|: > + seed->rand() < *readdir()* readdir({directory} [, {expr}]) @@ -7178,6 +7196,22 @@ sqrt({expr}) *sqrt()* Can also be used as a |method|: > Compute()->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. + - If {expr} is given, return seed values which x element is + {expr}. This is useful for testing or when a predictable + sequence is expected. + + Examples: > + :let seed = srand() + :let seed = srand(userinput) + :echo rand(seed) +< + Can also be used as a |method|: > + userinput->srand() + stdioopen({opts}) *stdioopen()* With |--headless| this opens stdin and stdout as a |channel|. May be called only once. See |channel-stdio|. stderr is not -- cgit From 061b06a8ae2e49e7921080ea4a1cc157e704bb28 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sun, 9 Jan 2022 23:31:48 +0000 Subject: docs(rand): cherry-pick changes from rt update 0c0734d https://github.com/vim/vim/commit/0c0734d527a132edfb4089be48486586424b3f41 --- runtime/doc/builtin.txt | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'runtime') diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index 35beb97518..8d88c533f0 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -5534,16 +5534,17 @@ range({expr} [, {max} [, {stride}]]) *range()* < rand([{expr}]) *rand()* Return a pseudo-random Number generated with an xorshift - algorithm using seed {expr}. {expr} can be initialized by - |srand()| and will be updated by rand(). - If {expr} is omitted, an internal seed value is used and - updated. + 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 + rand(). If {expr} is omitted, an internal seed value is used + and updated. Examples: > :echo rand() :let seed = srand() :echo rand(seed) - :echo rand(seed) + :echo rand(seed) % 16 " random number 0 - 15 < Can also be used as a |method|: > seed->rand() @@ -7199,7 +7200,8 @@ 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. + 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. -- cgit From c97614d98fc7ab040851b7fe1bc4cb575ce8c627 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sun, 9 Jan 2022 23:26:03 +0000 Subject: vim-patch:8.1.2356: rand() does not use the best algorithm Problem: rand() does not use the best algorithm. Solution: use xoshiro128** instead of xorshift. (Kaito Udagawa, closes vim/vim#5279) https://github.com/vim/vim/commit/f8c1f9200c4b50969a8191a4fe0b0d09edb38979 --- runtime/doc/builtin.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'runtime') diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index 8d88c533f0..327b8dc4d9 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -5533,7 +5533,7 @@ range({expr} [, {max} [, {stride}]]) *range()* GetExpr()->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 @@ -7200,11 +7200,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() -- cgit From 4f7a8991a93ddb1b6ab7cd8a8f21b5197c4612bb Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Mon, 10 Jan 2022 10:31:16 +0000 Subject: vim-patch:8.2.0233: crash when using garbagecollect() in between rand() Problem: Crash when using garbagecollect() in between rand(). Solution: Redesign the rand() and srand() implementation. (Yasuhiro Matsumoto, closes vim/vim#5587, closes vim/vim#5588) https://github.com/vim/vim/commit/4f645c54efe33d7a11e314676e503118761f08a7 Omit test_srand_seed. Unmacroify SHUFFLE_XOSHIRO128STARSTAR and SPLITMIX32 while we're at it (leave ROTL alone as it's fairly innocent). --- runtime/doc/vim_diff.txt | 1 + 1 file changed, 1 insertion(+) (limited to 'runtime') diff --git a/runtime/doc/vim_diff.txt b/runtime/doc/vim_diff.txt index 7e61eac404..11849632c5 100644 --- a/runtime/doc/vim_diff.txt +++ b/runtime/doc/vim_diff.txt @@ -574,6 +574,7 @@ Test functions: test_scrollbar() test_setmouse() test_settime() + test_srand_seed() TUI: *t_xx* *termcap-options* *t_AB* *t_Sb* *t_vb* *t_SI* -- cgit