如何批量制作具有一定字符长度的一系列随机字母和数字? [英] How to make a series of random letters and numbers with a certain character length in batch?
本文介绍了如何批量制作具有一定字符长度的一系列随机字母和数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试制作一个程序,该程序可以生成一系列随机的字母和数字,分别使用大写和小写字母.有什么办法可以使我的代码成为可能?
I am trying to make a program that makes a series of random letters and numbers, upper-case and lower case. Is there a way that I can make this possible for my code?
@echo off
title random password gen
:Start
cls && set choice=0
echo (type 0 to exit)
echo.
echo 1 Random Password
echo 5 Random Passwords
echo 10 Random Passwords
set /p input= quantity:
if %input%==0 exit
if %input%==1 set /a choice=%choice%+1 && goto a
if %input%==2 goto set /a choice=%choice%+5 && goto a
if %input%==3 goto set /a choice=%choice%+10 && goto a
goto start
:a
cls
if %choice%==1 echo your password is %random%
if %choice%==5 echo your 5 passwords are %random%, %random%, %random%, %random%, %random%.
if %choice%==10 echo your 10 passwords are %random%, %random%, %random%, %random%, %random%,%random%, %random%, %random%, %random%, %random%.
pause
echo press any key to continue to menu... && pause nul> && goto menu
推荐答案
- 您在成功使用
&&
时错误地使用了条件执行.到 一行上有两个命令,只能使用一个&
- %Random%返回0到32768之间的数字.请参见 ss64.com/nt/syntax-random.html
- 由于存在外部程序,因此选择var有点麻烦 choice.exe
- 如果您不将%random%存储到var中,它将丢失,下次%random% 得到不同的值.
- You are using the conditional execution on success
&&
wrongly. To have two commands on one line use only one&
- %Random% returns a number between 0 and 32768. See ss64.com/nt/syntax-random.html
- The var choice is a bit cumbersome as there is an external program choice.exe
- If you don't store %random% to a var it is lost, next time %random% gets a different value.
以下批次将生成随机的姓名/密码
The following batch will generate a random Name/Passwort
@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
Set "Chars=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
:: get one char from the first 26(Uppercase) and append to var Name
Call :RandChar 26 Name
:: get 14 chars from all Chars and append to var Name
For /L %%A in (1,1,14) Do Call :RandChar 62 Name
Echo %Name%
Goto :Eof
:RandChar Range Var
Set /A Pnt=%Random% %% %1
Set %2=!%2!!Chars:~%Pnt%,1!
Goto :Eof
powerShell中的相同之处稍微简单一些:
The same in powerShell is a bit simpler:
$chars = [char[]]"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
$Password = [string](($chars[0..25]|Get-Random)+(($chars|Get-Random -Count 14) -join ""))
这篇关于如何批量制作具有一定字符长度的一系列随机字母和数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文