Skip to content

[todo] add rand(), srand()#1277

Closed
mattn wants to merge 21 commits intovim:masterfrom
mattn:rand
Closed

[todo] add rand(), srand()#1277
mattn wants to merge 21 commits intovim:masterfrom
mattn:rand

Conversation

@mattn
Copy link
Member

@mattn mattn commented Nov 24, 2016

This add Xorshift random generator. In general implementation, srand() may be called from some plugins in several times. So it need to have statefull random generator like Xorshift. Below is an usage of srand().

:echo srand()
[123456789, 362436069, 521288629, 88675123]
:echo srand(-1)
[1479837050,, 362436069, 521288629, 88675123]
:echo srand(2)
[2, 362436069, 521288629, 88675123]

srand return four numbers as list. When expr is not given, the x element of xorshift become 123456789. When negative number is given, x become time(NULL). When positive number is given, x become it given.

Below is an usage of rand()

let a = srand()
:echo rand(a)
597902826
:echo rand(a)
458295558
:echo rand(a)
1779455562
:echo rand(a)
663552176
:echo rand(a)
507026878

@brammool
Copy link
Contributor

brammool commented Nov 24, 2016 via email

@mattn
Copy link
Member Author

mattn commented Nov 25, 2016

For example,

lorem ipsum generator

https://github.com/mattn/emmet-vim

This generate example strings. It's useful to confirm html/css with texts like below.

Ipsum in quidem aliquid quas obcaecati. Necessitatibus ullam excepturi deserunt numquam quod. Eligendi aperiam repellendus deserunt aliquam voluptate possimus ut quibusdam veniam, magni Exercitationem nesciunt hic esse asperiores suscipit Quidem!

random tips display

https://github.com/vim-scripts/Random-Tip-Displayer

This is useful to display random tips when vim startup.

random colorscheme

https://github.com/mattn/random_colorscheme-vim

This may be useful to change of pace, maybe. 😄

games

fun with vim. 🎮

invador https://github.com/mattn/invader-vim
samegame https://github.com/ynkdir/vim-samegame
mahjong https://github.com/mattn/mahjong-vim
tetris http://www.vim.org/scripts/script.php?script_id=172
sokoban http://www.vim.org/scripts/script.php?script_id=211
mines http://www.vim.org/scripts/script.php?script_id=551
lifegame http://www.vim.org/scripts/script.php?script_id=377
sudoku http://www.vim.org/scripts/script.php?script_id=3553
https://github.com/mattn/flappyvird-vim

@mattn
Copy link
Member Author

mattn commented Nov 25, 2016

In other case, useful to generate test data.

@kamichidu
Copy link

Somtimes I want to generate UUID for editing some files, html element id, visual studio solution file for example.
If I have these functions, I can create UUIDv4 implementation.

I know you probably say I can implement UUIDv1,2,3 and 5.
But I think UUIDv4 is better than others, since it reduce conflicts under actual stuation.

@mattn
Copy link
Member Author

mattn commented Nov 25, 2016

I don't think the naming rand/srand is good. If you have an idea, please let me know.

@chrisbra
Copy link
Member

+1 for a random generator. Considering there have been patches posted in the past already several times I think it is generally useful

@ZyX-I
Copy link

ZyX-I commented Nov 25, 2016

I also needed random number generator a few times, mostly imitating it with reltimestr() AFAIR.

@mattn
Copy link
Member Author

mattn commented Nov 25, 2016

Xorshift use full 32bit and Vim script deson't have unsigned number. So this seems not work on -num64.

@mattn
Copy link
Member Author

mattn commented Nov 25, 2016

Do you prefer xorshift() than rand() ?

@ichizok
Copy link
Contributor

ichizok commented Nov 25, 2016

I could be wrong, but;
what matters are, bit-pattern to be random, and calculating by logical-shift in c-code,
so Vim script perhaps can use random as singed even when without num64.

@mattn mattn changed the title add rand(), srand() [todo] add rand(), srand() Aug 10, 2017
@codecov-io
Copy link

Codecov Report

Merging #1277 into master will decrease coverage by <.01%.
The diff coverage is 58.92%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1277      +/-   ##
==========================================
- Coverage   78.84%   78.83%   -0.01%     
==========================================
  Files         105      105              
  Lines      142026   142082      +56     
==========================================
+ Hits       111976   112017      +41     
- Misses      30050    30065      +15
Impacted Files Coverage Δ
src/evalfunc.c 88.12% <58.92%> (-0.29%) ⬇️
src/ex_cmds.c 80.05% <0%> (-0.25%) ⬇️
src/channel.c 83.57% <0%> (-0.08%) ⬇️
src/message.c 76.41% <0%> (-0.05%) ⬇️
src/terminal.c 75.37% <0%> (+0.04%) ⬆️
src/os_unix.c 58.92% <0%> (+0.04%) ⬆️
src/ex_cmds2.c 84.98% <0%> (+0.09%) ⬆️
src/window.c 83.4% <0%> (+0.09%) ⬆️
src/gui_gtk_x11.c 48.47% <0%> (+0.09%) ⬆️
src/sign.c 92.65% <0%> (+0.13%) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 895d966...b5aa623. Read the comment docs.

@chrisbra
Copy link
Member

This pull request introduces 1 alert when merging 2520cda into b0e2da2 - view on LGTM.com

new alerts:

  • 1 for Comparison result is always the same

Comment posted by LGTM.com

@brammool
Copy link
Contributor

It's been a long time in the todo list, and finally bubbled up.
Instead of UINT32_TYPEDEF it should use UINT32_T.
I suppose it's OK to restrict to 32 bit numbers, to that the code using it is portable.
There could be a few more test cases, e.g. with an invalid argument to rand().

To be a bit more descriptive it should be called pseudo-random.
Can someone comment on the algorithm used?

@mattn
Copy link
Member Author

mattn commented Nov 25, 2019

Fixed implementation to be possible to use easily.

  • srand() without {expr} initialize global seed value
  • rand() without {expr} generate pseudo random value with global seed value
  • srand() with {expr} initialize seed value with x element by given value
  • rand() with {expr} generate pseudo random value with seed value given

Can someone comment on the algorithm used?

https://en.wikipedia.org/wiki/Xorshift

@umireon
Copy link

umireon commented Nov 25, 2019

xoshiro128** is better than the Marseglia's xorshift in the point of speed and equidistribution.
For detail, see http://prng.di.unimi.it/

@mattn
Copy link
Member Author

mattn commented Nov 25, 2019

@brammool Which do you prefer xorshift or xoshiro128**

@umireon
Copy link

umireon commented Nov 25, 2019

Initializing the state with a fixed number is not good because the similar bit pattern produces the similar random sequences.
For example, srand(1) and srand(2) produce similar sequences in spite of expectation.

The most famous state initialization algorithm might be MT's init_genrand, but I suggest that using SplitMix32 to fill the state.

The example of the proper state initialization is placed in vital.vim.

@brammool brammool closed this in 06b0b4b Nov 25, 2019
@brammool
Copy link
Contributor

brammool commented Nov 26, 2019 via email

@umireon
Copy link

umireon commented Nov 26, 2019

OK, I will.

seandewar added a commit to seandewar/neovim that referenced this pull request Jan 10, 2022
Problem:    Random number generator in Vim script is slow.
Solution:   Add rand() and srand(). (Yasuhiro Matsumoto, closes vim/vim#1277)
vim/vim@06b0b4b

vim_time and test_settime is N/A.
Add a modeline to test_random.vim.
seandewar added a commit to seandewar/neovim that referenced this pull request Jan 10, 2022
Problem:    Random number generator in Vim script is slow.
Solution:   Add rand() and srand(). (Yasuhiro Matsumoto, closes vim/vim#1277)
vim/vim@06b0b4b

Add missing method call usage to eval.txt.
vim_time and test_settime is N/A.
Add a modeline to test_random.vim.
seandewar added a commit to seandewar/neovim that referenced this pull request Jan 10, 2022
Problem:    Random number generator in Vim script is slow.
Solution:   Add rand() and srand(). (Yasuhiro Matsumoto, closes vim/vim#1277)
vim/vim@06b0b4b

Add missing method call usage to eval.txt.
vim_time and test_settime is N/A.
Add a modeline to test_random.vim.
seandewar added a commit to seandewar/neovim that referenced this pull request Jan 10, 2022
Problem:    Random number generator in Vim script is slow.
Solution:   Add rand() and srand(). (Yasuhiro Matsumoto, closes vim/vim#1277)
vim/vim@06b0b4b

Add missing method call usage to eval.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...
seandewar added a commit to seandewar/neovim that referenced this pull request Jan 10, 2022
Problem:    Random number generator in Vim script is slow.
Solution:   Add rand() and srand(). (Yasuhiro Matsumoto, closes vim/vim#1277)
vim/vim@06b0b4b

Add missing method call usage to eval.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).
seandewar added a commit to seandewar/neovim that referenced this pull request Jan 10, 2022
Problem:    Random number generator in Vim script is slow.
Solution:   Add rand() and srand(). (Yasuhiro Matsumoto, closes vim/vim#1277)
vim/vim@06b0b4b

Add missing method call usage to eval.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).
zeertzjq pushed a commit to seandewar/neovim that referenced this pull request Jan 31, 2022
Problem:    Random number generator in Vim script is slow.
Solution:   Add rand() and srand(). (Yasuhiro Matsumoto, closes vim/vim#1277)
vim/vim@06b0b4b

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).
seandewar added a commit to seandewar/neovim that referenced this pull request Feb 5, 2022
Problem:    Random number generator in Vim script is slow.
Solution:   Add rand() and srand(). (Yasuhiro Matsumoto, closes vim/vim#1277)
vim/vim@06b0b4b

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).
zbirenbaum pushed a commit to zbirenbaum/neovim that referenced this pull request Feb 11, 2022
Problem:    Random number generator in Vim script is slow.
Solution:   Add rand() and srand(). (Yasuhiro Matsumoto, closes vim/vim#1277)
vim/vim@06b0b4b

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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants