需要一行代码来吐出随机的指定字符串

时间:2011-04-28 20:20:11

标签: batch-file dos

我在记事本中工作,需要一行代码来吐出随机的字符行(我指定)。我还需要能够更改数字字符。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:6)

我真的不确定你究竟在问什么,但也许你想要一个批量文件,它会随机出现一行字符,这里有一个可以做到这一点的脚本。

示例输出:

C:\>rand_chars
XF3KqsBIFzi
C:\>rand_chars
UNx1eQ8MebihmizfIjHT4gc7O85uIOxBk5u8xZj8pnBBOf0jSygII4kNx7IUJA8nMchRKl1f6sQgJjB

代码:

@echo off & setlocal EnableDelayedExpansion

REM Change these values to whatever you want, or change the code to take them
REM as command-line arguments.  You must set CHARS_LEN to the string length
REM of the string in the CHARS variable.
REM 
REM This script generates a string of these characters at least
REM MIN_CHARS_IN_LINE chars long and at most MAX_CHARS_IN_LINE chars long.

SET CHARS=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
SET /A CHARS_LEN=26 + 26 + 10
SET /A MIN_CHARS_IN_LINE=5
SET /A MAX_CHARS_IN_LINE=79

REM Pick a random line length and output a random character until we reach that
REM length.

call:rand %MIN_CHARS_IN_LINE% %MAX_CHARS_IN_LINE%
SET /A LINE_LENGTH=%RAND_NUM%

SET LINE=
for /L %%a in (1 1 %LINE_LENGTH%) do (
    call:rand 1 %CHARS_LEN%
    SET /A CHAR_INDEX=!RAND_NUM! - 1
    CALL SET EXTRACTED_CHAR=%%CHARS:~!CHAR_INDEX!,1%%
    SET LINE=!LINE!!EXTRACTED_CHAR!
)
echo !LINE!

goto:EOF

REM The script ends at the above goto:EOF.  The following are functions.

REM rand()
REM Input: %1 is min, %2 is max.
REM Output: RAND_NUM is set to a random number from min through max.
:rand
SET /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1
goto:EOF