Question

I want to generate 10 random numbers in the buffer.

In Emacs I would do:

<start macro>(random limit) <eval lisp><newline><end macro> to define the macro,

and 9 <execute macro> to generate it 10 times.

Is there a way I can do this in vim?

Was it helpful?

Solution

is this ok for you?

:r! echo $RANDOM

then

9@:

if you have certain programming language env available on your OS, you can eval those statement too.

OTHER TIPS

Vim doesn't offer native random generator, however if you have vim compiled with Python, the following method will append a random digit at the end of your line:

:py import vim, random; vim.current.line += str(random.randint(0, 9))

Note: To check if your vim supports Python, try: :echo has('python') (1 for yes).

You can also use shell which offers $RANDOM variable as Kent suggested (works with bash/ksh/zsh) which returns a pseudorandom (0-32767), in example:

:put =system('echo $RANDOM')

or:

:r! od -An -td -N1 /dev/urandom

On Windows, you've to have Cygwin/MSYS/SUA installed, or use %RANDOM% variable as Carpetsmoker suggested.

If you don't have access to shell and Python, as for workaround, you use last few digits from the current timestamp, in example:

:put =reltimestr(reltime())[-2:]

Note: If you're using it quite often, write a simple function which will return reltimestr(reltime())[-4:].

Note: Above methods returns only a pseudorandom integer which should not be used to generate an encryption key.


To add more random numbers please press @: to repeat the command again. Or prefix with number (like 10@:) to add much more of random numbers separated by new lines.


Related:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top