How to have multiple sums in a variable in batch
我正在尝试创建一个可以在下面工作的矩阵),但是速度很慢,因为它必须循环每个数字。所以我想知道您是否可以仅打印"(随机数)%2"而没有多个%random%%% 2。
color 0a
:a
set /a mat=%random% %% 2
echo |set /p=%mat%
goto a
我发现这要快得多:
1 2 3 4 5 6 7 8 9 10 11 12 | @echo off if not"%1" =="max" start /MAX cmd /c %0 max & exit/b color 0a setlocal EnableDelayedExpansion :a set"addition=" for /L %%i in (1,1,256) do ( set /a mat=!random! %% 2 set addition=!addition!!mat! ) echo |set /p=%addition% goto a |
编辑:我现在看到评论中@Stephan链接到的答案非常相似,因此也可以归功于他。
编辑#2:
您可能也喜欢这个,具有所有字符,因此看起来有点" matrixy "
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | @echo off if not"%1" =="max" start /MAX cmd /c %0 max & exit/b color a setlocal EnableDelayedExpansion set"CHARS=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@@@###$$$%%%%%%*** " echo %CHARS%>x&for %%? in (x) do set /a strlength=%%~z? - 2&del x :a call :randomString addition 100"%chars%" %strlength% echo |set /p=%addition% goto a :randomString set"length=%2" set"CHARS=%~3" if ["%CHARS%"]==[""] set"CHARS=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@@@###$$$%%%%%%***" set"strlength=%4" if [%strlength%]==[] echo %CHARS%>x&for %%? in (x) do set /a strlength=%%~z? - 2&del x set"line=" for /L %%a in (1 1 %length%) do ( set /a"randnr=!random!%%!strlength!" for /l %%n in (!randnr! 1 !randnr!) do set"line=!line!!CHARS:~%%n,1!" ) set %1=%line% exit /b |
要获得额外效果,请在CHARS变量中添加6个空格。请注意,我还添加了一行以全屏模式启动它。
以下方法是解决此问题的最快方法:
1 2 3 4 5 6 7 8 9 10 11 | @echo off setlocal EnableDelayedExpansion rem Prepare the output for one line set"bin=0101010101" set"line=" for /L %%i in (1,1,79) do ( set"line=!line!%%bin:~^!random:~-1^!,1%%" ) for /L %%i in () do call echo %line% |