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
  • 23,334
  • 5
  • 52
  • 98
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

3 Answers3

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
  • 23,334
  • 5
  • 52
  • 98
2

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
  • 36
  • 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
  • 33
  • 4