如何批量获得随机字母输出
Posted
技术标签:
【中文标题】如何批量获得随机字母输出【英文标题】:How do i get a random letter output in batch 【发布时间】:2016-09-09 13:43:50 【问题描述】:我如何批量获得一个不需要更新的随机字母输出,每次它只需生成一个新的随机字母,就像这样
@echo off
title random letter gen
echo welcome
echo press any key to get a random letter
!!!!!!!!!!!!i want the %letter% to give a random letter!!!!!!!!!!
echo %letter%
echo press a key to get a new letter
pause
goto a
:a
echo %letter%
pause
goto a
【问题讨论】:
How to use random in BATCH script?的可能重复 OP,如果以下答案之一有帮助,请考虑将其标记为已接受。 See this Q/A post 解释为什么这很重要。 【参考方案1】:您还可以使用退出代码 ASCII 输出来输出小写字母和数字。
这是一个使用数字小写和大写字符的随机字符串生成器:
@ECHO OFF& SETLOCAL ENABLEDELAYEDEXPANSION
:RandomStrings
::: numbers: char 48 + (0-9)
::: lowercase: char 97 + (0-25)
::: uppercase: char 65 + (0-25)
SET "RANDSTUFF="
SET "CharCount=8"
FOR /L %%A in (1,1,%CharCount%) DO (
SET /A "Chance=%RANDOM% %% 3"
CALL :RandomStrings.!Chance!
)
ECHO.!RANDSTUFF!
EXIT /B
:RandomStrings.0
SET /A "nAscii=%RANDOM%*9/32768+48"
CMD /c EXIT /b %nAscii%
SET RANDSTUFF=!RANDSTUFF!%=ExitCodeASCII%
EXIT /B
:RandomStrings.1
SET /A "lAscii=%RANDOM%*26/32768+97"
CMD /c EXIT /b %lAscii%
SET RANDSTUFF=%RANDSTUFF%%=ExitCodeASCII%
EXIT /B
:RandomStrings.2
SET /A "uAscii=%RANDOM%*26/32768+65"
CMD /c EXIT /b %uAscii%
SET RANDSTUFF=%RANDSTUFF%%=ExitCodeASCII%
EXIT /B
【讨论】:
【参考方案2】:这是生成随机字母的快速方法。 %=ExitCodeAscii%
包含 %ERRORLEVEL%
的 ASCII 值。 More information.
rem // generate a random number between 65 and 90
set /a "ascii = %random% * 26 / 32768 + 65"
rem // set errorlevel to that number
cmd /c exit /b %ascii%
rem // get letter corresponding to that exit code
echo %=ExitCodeAscii%
【讨论】:
【参考方案3】:蛋车,
我在 Superuser.com 上找到了这段代码。它被设计为密码生成器,但您可以编辑为只允许它生成单个字符。
https://superuser.com/questions/349474/how-do-you-make-a-letter-password-generator-in-batch
希望这能满足您的需求。
@Echo Off
color 0a
set /P lengthnumberuser="What length do you want your password to be? "
pause
cls
Setlocal EnableDelayedExpansion
Set _RNDLength=%lengthnumberuser%
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
Echo Password is: is !_RndAlphaNum!
pause
【讨论】:
以上是关于如何批量获得随机字母输出的主要内容,如果未能解决你的问题,请参考以下文章