Answers:
有一个讨论,在这里,你可以为你的目的相适应。
@Echo Off
Setlocal EnableDelayedExpansion
Set _RNDLength=8
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 Random string is !_RndAlphaNum!
TheOutcaste解释了以上内容:
我已经对其进行了修改,因此您可以轻松指定长度并添加或删除字符,而无需更改代码的任何其他部分。
例如,您可能不想同时使用0和O(零和大写O)或1和l(一个小写L)。
除以下字符外,您可以使用标点符号:
! % ^ & < >
您可以使用
^
and%
,但必须在_Alphanumeric
变量中两次输入^^或%%
。但是,如果您想_RndAlphaNum
稍后在批处理文件中使用结果()(而不是回显屏幕),则可能需要特殊处理。您甚至可以使用空格,只要它不是字符串中的最后一个字符即可。但是,如果它以生成的字符串中的最后一个字符结尾,则不会使用它,因此您只有7个字符。
Set /A _RND=_RND%%61
-_RND先前被设置为随机数,然后对其进行模数61,以得到0到61之间的数字。SET _rndalphanum=!_Alphanumeric:~%_RND%,1!
然后,它在_RND位置为_Alphanumeric的子字符串1个字符。因此,它正在选择一个介于0到61之间的数字,然后从A-Za-z中提取该字符。然后,您可以通过重复该过程来构建随机字符的alpha字符串。
rem 16 stings pwd
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set alfanum=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
set pwd=
FOR /L %%b IN (0, 1, 16) DO (
SET /A rnd_num=!RANDOM! * 62 / 32768 + 1
for /F %%c in ('echo %%alfanum:~!rnd_num!^,1%%') do set pwd=!pwd!%%c
)
echo pwd=%pwd%
{ }
否则它将很难阅读。
这是一个简单而优雅的解决方案
@echo off
setlocal enableextensions enabledelayedexpansion
set /P _length=Password Length: %==%
set /a z = %_length%
set "string=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
set "password="
for /L %%i in (1,1,!z!) do call :_genRand
echo Password is: %password%
goto :EOF
:_genRand
set /a x=%random% %% 62
set password=%password%!string:~%x%,1!
goto :eof
这是我的。Ameture,但有效
@Echo OFF
@echo Would You Like Me To Make You A Password? Type Your Response And Press Enter.
set /p ans=
if %ans%==yes (goto yes
)
if %ans%==no (goto exit
)
:exit
cls
@echo Are You Sure?
set /p ans=
if %ans%==yes (goto exit
)
if %ans% NEQ yes (goto yes
)
:yes
cls
@echo Loading...
@echo OFF
ping localhost -n 4 > Nul
goto hardpart
:hardpart
@echo OFF
cls
@echo Get a peice of paper so you will remember
pause
cls
@echo OFF
set /A r=%RANDOM% %% 25 + 1
cls
if %r%==1 goto 1
if %r%==2 goto 2
if %r%==3 goto 3
if %r%==4 goto 4
if %r%==5 goto 5
if %r%==6 goto 6
if %r%==7 goto 7
if %r%==8 goto 8
if %r%==9 goto 9
if %r%==10 goto 10
if %r%==11 goto 11
if %r%==12 goto 12
if %r%==13 goto 13
if %r%==14 goto 14
if %r%==15 goto 15
if %r%==16 goto 16
if %r%==17 goto 17
if %r%==18 goto 18
if %r%==19 goto 19
if %r%==20 goto 20
if %r%==21 goto 21
if %r%==22 goto 22
if %r%==23 goto 23
if %r%==24 goto 24
if %r%==25 goto 25
if %r%==26 goto 26
:1
@echo 1st letter is a
goto number2
pause
goto number2
:2
@echo 1st letter is b
pause
goto number2
:3
@echo 1st letter is c
pause
goto number2
:4
@echo 1st letter is d
pause
goto number2
:5
@echo 1st letter is e
pause
goto number2
:6
@echo 1st letter is f
pause
goto number2
:7
@echo 1st letter is g
pause
goto number2
:8
@echo 1st letter is h
pause
goto number2
:9
@echo 1st letter is i
pause
goto number2
:10
@echo 1st letter is j
pause
goto number2
:11
@echo 1st letter is k
pause
goto number2
:12
@echo 1st letter is l
pause
goto number2
:13
@echo 1st letter is m
pause
goto number2
:14
@echo 1st letter is n
pause
goto number2
:15
@echo 1st letter is o
pause
goto number2
:16
@echo 1st letter is p
pause
goto number2
:17
@echo 1st letter is q
pause
goto number2
:18
@echo 1st letter is r
pause
goto number2
:19
@echo 1st letter is s
pause
goto number2
:20
@echo 1st letter is t
pause
goto number2
:21
@echo 1st letter is u
pause
goto number2
:22
@echo 1st letter is v
pause
goto number2
:23
@echo 1st letter is w
pause
goto number2
:24
@echo 1st letter is x
pause
goto number2
:25
@echo 1st letter is y
pause
goto number2
:26
@echo 1st letter is z
pause
goto number2
:number2
@echo OFF
set /A s=%RANDOM% %% 25 + 1
cls
if %s%==1 goto 1a
if %s%==2 goto 2a
if %s%==3 goto 3a
if %s%==4 goto 4a
if %s%==5 goto 5a
if %s%==6 goto 6a
if %s%==7 goto 7a
if %s%==8 goto 8a
if %s%==9 goto 9a
if %s%==10 goto 10a
if %s%==11 goto 11a
if %s%==12 goto 12a
if %s%==13 goto 13a
if %s%==14 goto 14a
if %s%==15 goto 15a
if %s%==16 goto 16a
if %s%==17 goto 17a
if %s%==18 goto 18a
if %s%==19 goto 19a
if %s%==20 goto 20a
if %s%==21 goto 21a
if %s%==22 goto 22a
if %s%==23 goto 23a
if %s%==24 goto 24a
if %s%==25 goto 25a
if %s%==26 goto 26a
:1a
@echo The next letter is a
pause
goto number2
:2a
@echo The next letter is b
pause
goto number2
:3a
@echo The next letter is c
pause
goto number2
:4a
@echo The next letter is d
pause
goto number2
:5a
@echo The next letter is e
pause
goto number2
:6a
@echo The next letter is f
pause
goto number2
:7a
@echo The next letter is g
pause
goto number2
:8a
@echo The next letter is h
pause
goto number2
:9a
@echo The next letter is i
pause
goto number2
:10a
@echo The next letter is j
pause
goto number2
:11a
@echo The next letter is k
pause
goto number2
:12a
@echo The next letter is l
pause
goto number2
:13a
@echo The next letter is m
pause
goto number2
:14a
@echo The next letter is n
pause
goto number2
:15a
@echo The next letter is o
pause
goto number2
:16a
@echo The next letter is p
pause
goto number2
:17a
@echo The next letter is q
pause
goto number2
:18a
@echo The next letter is r
pause
goto number2
:19a
@echo The next letter is s
pause
goto number2
:20a
@echo The next letter is t
pause
goto number2
:21a
@echo The next letter is u
pause
goto number2
:22a
@echo The next letter is v
pause
goto number2
:23a
@echo The next letter is w
pause
goto number2
:24a
@echo The next letter is x
pause
goto number2
:25a
@echo The next letter is y
pause
goto number2
:26a
@echo The next letter is z
pause
goto number2
{ }
否则它将很难阅读。
我编辑了保罗一:
@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
:generator
@Echo Off
Setlocal EnableDelayedExpansion
Set _RNDLength=8
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 Random string is !_RndAlphaNum!
这可以工作,但是当我们将其作为sub调用时,它的行为并不正确。(即,当您调用子例程:generator时,意外发生故障。
这是一个完整的版本,具有16个字符,包括大写,小写和数字。这是一个新手版本,因此它不包含“ for”之类的任何复杂命令,但效果很好。如果生成的行过多,它可能会滞后并偶尔停止工作,这只是缺点。
@echo off
title Random
:trigger
set trigger=16
:genKeys
set /a trigger=%trigger% - 1
set /a choose=(%random%*3/32767)+1
if %choose% == 1 goto lowercase
if %choose% == 2 goto uppercase
if %choose% == 3 goto number
:lowercase
set /a lower=(%random%*25/32767)+0
if %lower% == 0 set gen%trigger%=a
if %lower% == 1 set gen%trigger%=b
if %lower% == 2 set gen%trigger%=c
if %lower% == 3 set gen%trigger%=d
if %lower% == 4 set gen%trigger%=e
if %lower% == 5 set gen%trigger%=f
if %lower% == 6 set gen%trigger%=g
if %lower% == 7 set gen%trigger%=h
if %lower% == 8 set gen%trigger%=i
if %lower% == 9 set gen%trigger%=j
if %lower% == 10 set gen%trigger%=k
if %lower% == 11 set gen%trigger%=l
if %lower% == 12 set gen%trigger%=m
if %lower% == 13 set gen%trigger%=n
if %lower% == 14 set gen%trigger%=o
if %lower% == 15 set gen%trigger%=p
if %lower% == 16 set gen%trigger%=q
if %lower% == 17 set gen%trigger%=r
if %lower% == 18 set gen%trigger%=s
if %lower% == 19 set gen%trigger%=t
if %lower% == 20 set gen%trigger%=u
if %lower% == 21 set gen%trigger%=v
if %lower% == 22 set gen%trigger%=w
if %lower% == 23 set gen%trigger%=x
if %lower% == 24 set gen%trigger%=y
if %lower% == 25 set gen%trigger%=z
if %trigger% == 0 goto keys
goto genKeys
:uppercase
set /a upper=(%random%*25/32767)+0
if %upper% == 0 set gen%trigger%=A
if %upper% == 1 set gen%trigger%=B
if %upper% == 2 set gen%trigger%=C
if %upper% == 3 set gen%trigger%=D
if %upper% == 4 set gen%trigger%=E
if %upper% == 5 set gen%trigger%=F
if %upper% == 6 set gen%trigger%=G
if %upper% == 7 set gen%trigger%=H
if %upper% == 8 set gen%trigger%=I
if %upper% == 9 set gen%trigger%=J
if %upper% == 10 set gen%trigger%=K
if %upper% == 11 set gen%trigger%=L
if %upper% == 12 set gen%trigger%=M
if %upper% == 13 set gen%trigger%=N
if %upper% == 14 set gen%trigger%=O
if %upper% == 15 set gen%trigger%=P
if %upper% == 16 set gen%trigger%=Q
if %upper% == 17 set gen%trigger%=R
if %upper% == 18 set gen%trigger%=S
if %upper% == 19 set gen%trigger%=T
if %upper% == 20 set gen%trigger%=U
if %upper% == 21 set gen%trigger%=V
if %upper% == 22 set gen%trigger%=W
if %upper% == 23 set gen%trigger%=X
if %upper% == 24 set gen%trigger%=Y
if %upper% == 25 set gen%trigger%=Z
if %trigger% == 0 goto keys
goto genKeys
:number
set /a number=(%random%*9/32767)+0
set gen%trigger%=%number%
goto genKeys
:keys
cls
echo %gen15%%gen14%%gen13%%gen12%%gen11%%gen10%%gen9%%gen8%%gen7%%gen6%%gen5%%gen4%%gen3%%gen2%%gen1%%gen0%
pause >nul
goto trigger