Random 本机Vim随机数脚本

Random 本机Vim随机数脚本,random,vim,Random,Vim,我知道有多种方法可以获得随机数,例如从外壳中获得随机数。然而,我在android手机上运行vim,编译的代码很少。此外,它不必是严格随机的。关键是,在vim中获得一个相当好的随机数序列的有趣的、简洁的、快速的(也就是说,使用vim本机函数)或短方法是什么 function Rand() return str2nr(matchstr(reltimestr(reltime()), '\v\.@<=\d+')[1:]) endfunction 函数Rand() 返回str2nr(mat

我知道有多种方法可以获得随机数,例如从外壳中获得随机数。然而,我在android手机上运行vim,编译的代码很少。此外,它不必是严格随机的。关键是,在vim中获得一个相当好的随机数序列的有趣的、简洁的、快速的(也就是说,使用vim本机函数)或短方法是什么

function Rand()
    return str2nr(matchstr(reltimestr(reltime()), '\v\.@<=\d+')[1:])
endfunction
函数Rand()

返回str2nr(matchstr(reltimestr(reltime()),“\v\。@我最近在Vim脚本中使用了随机数。下面是我在这个过程中找到的一些资源

没有Vim脚本 无论如何,如果可以的话,请使用一个外部随机数生成器。通常,它们比Vim脚本中可以完成的任何事情都要好、更快

" generate a random integer from range [Low, High] using Python
function! RandInt(Low, High) abort
" if you use Python 3, the python block should start with `python3` instead of
" `python`, see https://github.com/neovim/neovim/issues/9927
python3 << EOF
import vim
import random

# using vim.eval to import variable outside Python script to python
idx = random.randint(int(vim.eval('a:Low')), int(vim.eval('a:High')))

# using vim.command to export variable inside Python script to vim script so
# we can return its value in vim script
vim.command("let index = {}".format(idx))
EOF

return index
endfunction
例如,试试看

  • :python随机导入;随机打印.randrange(1,7)
  • :echo系统('echo$RANDOM')
  • 另一种脚本语言
图书馆 Vim脚本库。希望它们能够提供高质量的RNG实现

  • 是vim jp用户组创建的一个优秀而全面的库。他们的随机数生成器具有令人印象深刻的一系列功能,是我所知道的最好的纯vim脚本RNG。vital.vim使用。请查看

    使用vital.vim滚动模具:

    let Random = vital#of('vital').import('Random')
    echo Random.range(1, 7)
    
  • 是一个小型随机数生成器插件。它导出了几个依赖于的全局函数。该项目似乎正在进行中

    使用rng滚动模具:

    echo RandomNumber(1, 6)
    
  • 是我自己的小整数库。我最近添加了一个随机数生成器,可以生成任意大小的整数。它使用

    使用magnum.vim滚动模具:

    let six = magnum#Int(6)
    echo magnum#random#NextInt(six).Add(magnum#ONE).Number()
    
  • 比其他库存在的时间要长得多。它的功能作为两个全局函数公开。使用Rndm滚动模具:

    echo Urndm(1, 6)
    
讨论和片段 最后,提供一些有见地的讨论和Vim脚本片段的链接

  • 在本页上

  • loreb的在Vim脚本中有大量RNG实现。非常有用

  • 有两个Vim脚本片段。Bee-9给出的第一个片段限制为16位,但我发现它非常有效。如下所示:

    let g:rnd = localtime() % 0x10000
    
    function! Random(n) abort
      let g:rnd = (g:rnd * 31421 + 6927) % 0x10000
      return g:rnd * a:n / 0x10000
    endfunction
    
  • ,在名为Bart的个人配置文件中找到

  • 讨论了Vim的“表达式寄存器”,并通篇引用了随机数示例。引用了这个Stackoverflow问题和ZyX的代码段。推荐使用

  • wikia上的Vim wiki有一篇文章,其中包含一些尚未提及的资源


根据其他人的答案和互联网上的其他资源,我写了 用于在给定范围内生成随机整数的两个函数
[低、高]

这两个函数都接收两个参数:
Low
High
,并返回一个 此范围内的随机数

结合Python和Vim脚本 第一个函数结合了Python和Vim脚本

" generate a random integer from range [Low, High] using Python
function! RandInt(Low, High) abort
" if you use Python 3, the python block should start with `python3` instead of
" `python`, see https://github.com/neovim/neovim/issues/9927
python3 << EOF
import vim
import random

# using vim.eval to import variable outside Python script to python
idx = random.randint(int(vim.eval('a:Low')), int(vim.eval('a:High')))

# using vim.command to export variable inside Python script to vim script so
# we can return its value in vim script
vim.command("let index = {}".format(idx))
EOF

return index
endfunction
使用
luaeval()
仅限Neovim) 生成随机数的第三种方法是使用lua via

如果要在非严重情况下生成随机数,可以使用
这些方法是一个开始。

当然!我知道reltime,但认为它“不够随机”。但它似乎给出了一个非常好的、均匀的分布。我试图用预生成的随机表做一些事情,但我无法找到一种快速简单的方法来做。确实,这还不够随机。当值低于10时,我检查了该代码最后两个数的1000倍输出。这是结果:00=20,01=29,02=63, 03= 88, 04=104, 05=126, 06=152, 07=123, 08=153, 09=142 (第一个值=最后2个数字,最后一个值=它出现的次数。05到09之间的值比01到09之间的值多得多05@Remonn因为任何基于时间的解决方案都取决于您的系统。在我的系统上,分布是统一的:。@ZyX,你好,ZyX,是的,我就是这么做的。这是我的代码:
用于范围(11000)内的I| let newrandom=str2nr(localtime)| while | matchstr(newrandom,“\d\d$”)>9 | let newrandom=str2nr(localtime)| endwhile | put=newrandom | endfor
@remonthere
system()
函数。另一个脚本的链接现在已断开。与vi.stackexchange.com相关
" math.randomseed() is need to make the random() function generate different numbers 
" on each use. Otherwise, the first number it generate seems same all the time.
luaeval('math.randomseed(os.time())')
let num = luaeval('math.random(1, 10)')