How to use random in BATCH script?
- IAdapter
- 2011-04-25 10:18
- 9
How to use random in BATCH script?
9 Answers
%RANDOM% gives you a random number between 0 and 32767.
You can control the number's range with:
set /a num=%random% %%100
- will produce number between 0~99.
This one:
set /a num=%random% %%100 +1
- will produce number between 1~100.
batch file random number between 1 and 100, Your first code creates random numbers between 0 and 99, there is no incrementing – jeb Feb 28 '14 at 23:34. 1. Please show us the real code you are using. What you've posted, other than generating and displaying random numbers 0..99, not 1..100 works perfectly. – Magoo Mar 1 '14 at 1:54. %RANDOM% gives you a random number between 0 and 32767. Using an expression like SET /A test=%RANDOM% * 100 / 32768 + 1 , you
GaryNg
2014-06-05 13:26
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_NUM after that function exits.
@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
batch random number between 1 and 5, Let's say you want a number 1-5; you could use the following: :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. %RANDOM% gives you a random number between 0 and 32767. Using an expression like SET /A test=%RANDOM% * 100 / 32768 + 1 , you
indiv
2011-04-28 18:50
@echo off & setLocal EnableDelayedExpansion for /L %%a in (1 1 100) do ( echo !random! )
batch random string, I am trying to make a random string in batch, and I have no idea what to do. I have called a random string of numbers with the %random% function, but I have no idea how to permanently replace certain characters in my variable: To assign a variable a value, use the SET command: SET subkey1=%random%%random%%random%%random%%random%%random%
Evgeny Gavrin
2011-04-25 10:21
You could do it this way, which does not require EnableDelayedExpansion
:choosenamea cls set /a choosemname=%random% if %choosemname% GTR %max% goto choosenameb if %choosemname% LSS %min% goto choosenameb goto gotnamenow
where max is your max and min is 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.
batch file random value, %RANDOM% gives you a random number between 0 and 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]). %RANDOM% gives you a random number between 0 and 32767. Using an expression like SET /A test=%RANDOM% * 100 / 32768 + 1 , you
calebhk98
2018-09-17 20:55
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!
batch file random delay, The most obvious way to pause a batch file is of course the PAUSE command. This will stop execution of the batch file until someone presses "any key". Well, almost any key: Ctrl, Shift, NumLock etc. won't work. This is fine for interactive use, but sometimes we just want to delay the batch file for a fixed number of seconds, without user interaction. %RANDOM% gives you a random number between 0 and 32767. Using an expression like SET /A test=%RANDOM% * 100 / 32768 + 1 , you
Jacob
2013-07-14 12:45
@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
dos batch file random number, %RANDOM% gives you a random number between 0 and 32767. You can control the number's range with: set /a num=%random% %%100 - will produce number between 0~99. This one: set /a num=%random% %%100 +1 - will produce number between 1~100. This can be problematic when running a batch file, if the script always takes about the same time to run before calling %RANDOM% then the number returned will
Sinji58
2014-02-25 12:14
Let's say you want a number 1-5; you could use the following:
: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:
: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.
get random number batch file, batch-file Random Numbers Example #. Using the dynamic variable %Random%, we can get a random integer from 0 to 32767. This obviously, Generating Random Numbers Within Specific Range #. The basic method to do so is listed below. Note that you will not get Generating Random Numbers larger batch-file Random Numbers. Example#. Using the dynamic variable %Random% , we can get a random
user3542843
2014-04-16 20:19
@(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 ((()))
random value in batch, %RANDOM% gives you a random number between 0 and 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]). How-to: Generate Random Numbers. The Windows CMD shell contains a built-in variable called %RANDOM% that can be used to generate random numbers.
simple
2014-12-27 07:19

%RANDOM%gives you a random number between 0 and 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]).Random Numbers - Windows CMD, For example if you are drawing random integer numbers where each number is This can be problematic when running a batch file, if the script always takes batch-file Random Numbers. Example#. Using the dynamic variable %Random% , we can get a random
mousio
2011-04-25 10:46