0

How do i get a random letter output in batch that dose not need to update each time it just makes a new random letter soo something like this

    @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
rojo
  • 24,000
  • 5
  • 55
  • 101
eggsedan
  • 19
  • 1
  • 1
  • 4
  • 1
    Possible duplicate of [How to use random in BATCH script?](http://stackoverflow.com/questions/5777400/how-to-use-random-in-batch-script) – Jehy May 13 '16 at 12:35
  • OP, if one of the answers below was helpful, please consider marking it as accepted. [See this Q/A post](http://meta.stackexchange.com/q/5234/275822) for an explanation why this is important. – rojo Jun 25 '16 at 01:13

4 Answers4

5

Here's a quick way to generate a random letter. %=ExitCodeAscii% contains the ASCII value of %ERRORLEVEL%. 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%
rojo
  • 24,000
  • 5
  • 55
  • 101
3

Eggsedan,

I found this string of code on Superuser.com. It is designed to be a password generator but you could edit to only allow it to make a single character.

https://superuser.com/questions/349474/how-do-you-make-a-letter-password-generator-in-batch

Hopefully this will meet your needs.

    @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
Community
  • 1
  • 1
Haveycode
  • 46
  • 5
1

You can also use The Exit code ASCII output to output lowercase and numbers.

Here's a Random String generator using numbers lowercase and uppercase chars:

@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
ThisLimn0
  • 43
  • 5
0

here is one that asks you the lenght of the string

@echo off
Title string generator V1
:DebLoop
set /a RN=%random%/100 *3 *4 /3 *9 /3 /9 +10 -5 *3 *2 
set UCN=0


:UCSLN
echo please select the lenght of the string to generate
set /p "UCSL=User Chosen String Lenght>"
if not %UCSL% GTR 0 (
echo invalid input, please chose a number above 0 
goto UCSLN
) else (
set /a UCSL=%UCSL%+1
echo generating string, please wait
:UCSL
set CN=0
set N=1
set /a RN=%random%/100 *3 *4 /3 *9 /3 /9 +10 -5 *3 *2 
if %RN% LEQ 0 goto UCSL
set OL=%OL%%L%
set /a UCN=%UCN%+1
if %UCN%==%UCSL% (
echo string generated:
echo %OL%>string.txt
string.txt
timeout 2 >nul
goto DebLoop
)
)
:GenLoop
set /a N=%N%+1
if %N% GEQ 62 set N=0
set /a CN=%CN%+1
if %CN%==%RN% (
set CH=%N%
goto DefLoop
)
goto GenLoop
:DefLoop
if %CH% GEQ 1 if %CH% LEQ 26 goto UpperCase
if %CH% GEQ 27 if %CH% LEQ 52 goto LowerCase
if %CH% GEQ 58 if %CH% LEQ 62 goto UpperThanFive
if %CH% GEQ 57 if %CH% LEQ 52 goto LowerThanFive

:UpperCase
if %CH% LEQ 13 goto UCFirst13
if %CH% GEQ 14 goto UCLast13

:LowerCase
if %CH% LEQ 39 goto LCFirst13
if %CH% GEQ 40 goto LCLast13

:UCFirst13
if %CH%==1 set L=A
if %CH%==2 set L=B
if %CH%==3 set L=C
if %CH%==4 set L=D
if %CH%==5 set L=E
if %CH%==6 set L=F
if %CH%==7 set L=G
if %CH%==8 set L=H
if %CH%==9 set L=I
if %CH%==10 set L=J
if %CH%==11 set L=K
if %CH%==12 set L=L
if %CH%==13 set L=M

:UCLast13
if %CH%==14 set L=N
if %CH%==15 set L=O
if %CH%==16 set L=P
if %CH%==17 set L=Q
if %CH%==18 set L=R
if %CH%==19 set L=S
if %CH%==20 set L=T
if %CH%==21 set L=U
if %CH%==22 set L=V
if %CH%==23 set L=W
if %CH%==24 set L=X
if %CH%==25 set L=Y
if %CH%==26 set L=Z

:LCFirst13
if %CH%==27 set L=a
if %CH%==28 set L=b
if %CH%==29 set L=c
if %CH%==30 set L=d
if %CH%==31 set L=e
if %CH%==32 set L=f
if %CH%==33 set L=g
if %CH%==34 set L=h
if %CH%==35 set L=i
if %CH%==36 set L=j
if %CH%==37 set L=k
if %CH%==38 set L=l
if %CH%==39 set L=m
:LCLast13
if %CH%==40 set L=n
if %CH%==41 set L=o
if %CH%==42 set L=p
if %CH%==43 set L=q
if %CH%==44 set L=r
if %CH%==45 set L=s
if %CH%==46 set L=t
if %CH%==47 set L=u
if %CH%==48 set L=v
if %CH%==49 set L=w
if %CH%==50 set L=x
if %CH%==51 set L=y
if %CH%==52 set L=z

:LowerThanFive
if %CH%==53 set L=1
if %CH%==54 set L=2
if %CH%==55 set L=3
if %CH%==56 set L=4
if %CH%==57 set L=5

:UpperThanFive
if %CH%==58 set L=6
if %CH%==59 set L=7
if %CH%==60 set L=8
if %CH%==61 set L=9
if %CH%==62 set L=0
goto UCSL




pause