0

I have looked up how to get a random letter form batch but it only gives me the same letter and rally changes.

here is the code

@echo off
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%
pause

the out put was this

R

Press any key to continue . . .

but, I was looking for more then just this one every time I use it.

Community
  • 1
  • 1
  • 3
    I can't close this as a duplicate of https://stackoverflow.com/q/56398593/4158862 since that has no accepted answer, but it's the same issue. The short version is that you should always run the script from the command prompt instead of double-clicking it. Also, I'm genuinely surprised that this works at all since you've created a variable called `%ascii %` because spaces are significant in `set ` statements, but this somehow works when you run it from the command line. – SomethingDark Jun 11 '19 at 03:54
  • 3
    I think the worst part is that I knew that, but my brain saw the quotes and decided to ignore the `/a` altogether. Regardless, putting spaces in `set` statements is a terrible idea and should be avoided whenever possible. – SomethingDark Jun 11 '19 at 04:18
  • @somethingdark `set /a` does not consider the space before `=` it sort of mimics an actual equation. `Set /a 5 = 2 + 3` however I always recommend using no spaces.. Just incase. – Gerhard Jun 11 '19 at 04:19
  • 1
    @somethingdark. Yes indeed. No spaces in batch `set` are recommended. – Gerhard Jun 11 '19 at 04:20

0 Answers0