How to generate a random number between 1 and 100

2019-09-19 09:05发布

问题:

%random% seems to go in order.

@ECHO OFF
SET /A RAND=%RANDOM% %%100
ECHO %RAND%
ECHO.

If you keep running this it increments until it reaches 100 and then the number start over. If it were random it would jump around.

@ECHO OFF
SET /A RAND=%RANDOM%
ECHO %RAND%
ECHO.

回答1:

SET /A RAND=%RANDOM%%%100+1

this may work.



回答2:

If I understoof the question right, here's what you're looking for.

echo off
title Number from 1 to 100.
color 0a
cls

:loop
cls
set /a rand=%random% %%101
echo %rand%
pause >nul
goto loop


回答3:

hey if the problem still persists
use this code
this generates a number between a and b
tweak it to your needs

@echo off       
color 02  
echo enter value of A  
set  /p a=  
echo.   
echo  enter value of B  
set /p b=   
:main  
set no=%random%  
if  %no% GEQ %a% goto sub  
if not %no% GEQ %a% goto  main  
:sub  
if %no% LEQ  %b% goto end  
if not %no% LEQ  %b% goto  main  
:end  
echo %no%   
goto main