windows 如何在 BATCH 脚本中使用随机数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5777400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:19:50  来源:igfitidea点击:

How to use random in BATCH script?

windowsrandomwindows-xpbatch-file

提问by IAdapter

How to use random in BATCH script?

如何在 BATCH 脚本中使用随机数?

回答by mousio

%RANDOM%gives you a random number between 0 and 32767.

%RANDOM%给你一个 0 到 32767 之间的随机数。

Using an expression like SET /A test=%RANDOM% * 100 / 32768 + 1, you can change the range to anything you like (here the range is [1…100] instead of [0…32767]).

使用类似 的表达式SET /A test=%RANDOM% * 100 / 32768 + 1,您可以将范围更改为您喜欢的任何内容(这里的范围是 [1…100] 而不是 [0…32767])。

回答by GaryNg

%RANDOM% gives you a random number between 0 and 32767.

%RANDOM% 为您提供 0 到 32767 之间的随机数。

You can control the number's range with:

您可以通过以下方式控制数字的范围:

set /a num=%random% %%100

- will produce number between 0~99.

- 将产生0~99之间的数字。

This one:

这个:

set /a num=%random% %%100 +1

- will produce number between 1~100.

- 将产生 1~100 之间的数字。

回答by indiv

You'll probably want to get several random numbers, and may want to be able to specify a different range for each one, so you should define a function. In my example, I generate numbers from 25 through 30 with call:rand 25 30. And the result is in RAND_NUMafter that function exits.

您可能希望获得多个随机数,并且可能希望能够为每个随机数指定不同的范围,因此您应该定义一个函数。在我的示例中,我使用call:rand 25 30. 结果是在RAND_NUM该函数退出后。

@echo off & setlocal EnableDelayedExpansion

for /L %%a in (1 1 10) do (
        call:rand 25 30
        echo !RAND_NUM!
)

goto:EOF

REM The script ends at the above goto:EOF.  The following are functions.

REM rand()
REM Input: %1 is min, %2 is max.
REM Output: RAND_NUM is set to a random number from min through max.
:rand
SET /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1
goto:EOF

回答by Evgeny Gavrin

@echo off & setLocal EnableDelayedExpansion

for /L %%a in (1 1 100) do (
echo !random!
)

回答by calebhk98

You could do it this way, which does not require EnableDelayedExpansion

你可以这样做,这不需要 EnableDelayedExpansion

:choosenamea
cls
set /a choosemname=%random%

if %choosemname% GTR %max% goto choosenameb
if %choosemname% LSS %min% goto choosenameb
goto gotnamenow

where maxis your max and minis your minimum. This is not very efficient since might take a lot of rounds if your range is too small. Also, this will not work for numbers larger than 32767.

max你的最大值在哪里,你的最小值在哪里min。这不是很有效,因为如果您的范围太小,可能需要很多回合。此外,这不适用于大于 32767 的数字。

回答by Jacob

set /a number=%random% %% [maximum]-[minimum]

example "

例子 ”

set /a number=%random% %% 100-50

will give a random number between 100 and 50. Be sure to only use one percentage sign as the operand if you are not using the line in a batch script!

将给出一个 100 到 50 之间的随机数。如果您不在批处理脚本中使用该行,请确保只使用一个百分号作为操作数!

回答by Sinji58

@echo off
title Professional Hacker
color 02
:matrix
echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%
echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%% 
echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%
echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%
echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%
echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%
echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%% 
echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%% 
goto matrix

回答by user3542843

Let's say you want a number 1-5; you could use the following:

假设您想要一个数字 1-5;您可以使用以下内容:

    :LOOP
    set NUM=%random:~-1,1%
    if %NUM% GTR 5 (
    goto LOOP )
    goto NEXT

Or you could use :~1,1 in place of :~-1,1. The :~-1,1 is not needed, but it greatly reduces the amount of time it takes to hit the right range. Let's say you want a number 1-50, we need to decide between 2 digits and 1 digit. Use:

或者您可以使用 :~1,1 代替 :~-1,1。:~-1,1 不是必需的,但它大大减少了达到正确范围所需的时间。假设您想要一个数字 1-50,我们需要在 2 位数和 1 位数之间做出决定。用:

    :LOOP
    set RAN1=%random:~-1,1%
    if %RAN1% GTR 5 (
    goto 1 )
    if %RAN1%==5 (
    goto LOOP )
    goto 2

    :1
    set NUM=%random:~-1,1%
    goto NEXT

    :2
    set NUM=%random:~-1,2%
    goto NEXT

You can add more to this algorithm to decide between large ranges, such as 1-1000.

您可以向此算法添加更多内容以在大范围(例如 1-1000)之间做出决定。

回答by simple

@(IF not "%1" == "max" (start /MAX cmd /Q /C %0 max&X)ELSE set C=1&set D=2&wmic process where name="cmd.exe" CALL setpriority "REALTIME">NUL)&CLS
:Y
title %random%6%random%%random%%random%%random%9%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%&color %D%&ECHO %random%%C%%random%%random%%random%%random%6%random%9%random%%random%%random%%random%%random%%random%%random%%random%%random%&(IF %C% EQU 46 (TIMEOUT /T 1 /NOBREAK>nul&set C=1&CLS&IF %D% EQU 9 (set D=1)ELSE set /A D=%D%+1)ELSE set /A C=%C%+1)&goto Y

simplified with multiple IF statements and plenty of ((()))

使用多个 IF 语句和大量 ((())) 进行简化

回答by strance

And just to be completely random for those who don't always want a black screen.

对于那些并不总是想要黑屏的人来说,这只是完全随机的。

@(IF not "%1" == "max" (start /MAX cmd /Q /C %0 max&X)ELSE set A=0&set C=1&set V=A&wmic process where name="cmd.exe" CALL setpriority "REALTIME">NUL)&CLS
:Y
(IF %A% EQU 10 set A=A)&(IF %A% EQU 11 set A=B)&(IF %A% EQU 12 set A=C)&(IF %A% EQU 13 set A=D)&(IF %A% EQU 14 set A=E)&(IF %A% EQU 15 set A=F)
(IF %V% EQU 10 set V=A)&(IF %V% EQU 11 set V=B)&(IF %V% EQU 12 set V=C)&(IF %V% EQU 13 set V=D)&(IF %V% EQU 14 set V=E)&(IF %V% EQU 15 set V=F)
(IF %A% EQU %V% set A=0)
title %A%%V%%random%6%random%%random%%random%%random%9%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%&color %A%%V%&ECHO %random%%C%%random%%random%%random%%random%6%random%9%random%%random%%random%%random%%random%%random%%random%%random%%random%&(IF %C% EQU 46 (TIMEOUT /T 1 /NOBREAK>nul&set C=1&CLS&SET /A A=%random% %%15 +1&SET /A V=%random% %%15 +1)ELSE set /A C=%C%+1)&goto Y

This will change screen color also both are random.

这将改变屏幕颜色也是随机的。