Batch file CMD批处理文件简单随机
使用.bat文件,我知道如何用%random%回传出一个随机数。如何设置%random%的特定随机范围,例如50-100?哦,我有一个积分系统,它在一开始就说明: 设置/A主分数=0 我如何设置要从主分数中加/减的随机范围数? 谢谢。这将有助于:Batch file CMD批处理文件简单随机,batch-file,range,Batch File,Range,使用.bat文件,我知道如何用%random%回传出一个随机数。如何设置%random%的特定随机范围,例如50-100?哦,我有一个积分系统,它在一开始就说明: 设置/A主分数=0 我如何设置要从主分数中加/减的随机范围数? 谢谢。这将有助于: # If you need a random int within a certain range, use the 'modulo' operator. # This returns the remainder of a division opera
# If you need a random int within a certain range, use the 'modulo' operator.
# This returns the remainder of a division operation.
RANGE=500
echo
number=$RANDOM
let "number %= $RANGE"
# ^^
echo "Random number less than $RANGE --- $number"
# If you need a random integer greater than a lower bound,
#+ then set up a test to discard all numbers below that.
FLOOR=200
number=0 #initialize
while [ "$number" -le $FLOOR ]
do
number=$RANDOM
done
echo "Random number greater than $FLOOR --- $number"
echo
# Let's examine a simple alternative to the above loop, namely
# let "number = $RANDOM + $FLOOR"
# That would eliminate the while-loop and run faster.
# But, there might be a problem with that. What is it?
# Combine above two techniques to retrieve random number between two limits.
number=0 #initialize
while [ "$number" -le $FLOOR ]
do
number=$RANDOM
let "number %= $RANGE" # Scales $number down within $RANGE.
done
echo "Random number between $FLOOR and $RANGE --- $number"
摘自这里:
然后,一旦您有了$number,您就可以执行以下操作:
num=expr$number+$MAINSCORE
希望能有所帮助。这个
%RANDOM%
返回一个介于0和32767之间的数字。要缩小这些范围,请使用模运算符,并使用加法或减法来抵消结果。例如:
@set /a bottomlimit = 50
@set /a upperlimit = 100
@set /a result = %bottomlimit% + %RANDOM% %% (%upperlimit% - %bottomlimit% + 1)
@echo %result%
就我个人而言,我真的很喜欢我的做事方式
这将经历5到10之间的任何时间
最重要的是,它很干净。谢谢你一个简单的程序,它允许你为一个简单的选择程序设置数字的数量
:loop
cls
goto before
:before
@echo off
setlocal delayexpansion
cls
@mod con: clos=80 lines=25
title Number Picker
color 0a
cls
goto menu
:menu
cls
echo.
echo Number Picker
echo.
set/p input= "Number: "
if %input%== goto show
:show
set /a bottomlimit = 0
set /a upperlimit = %input%
set /a result = %bottomlimit% + %random% %% (%upperlimit% - %bottomlimit% +1)
echo.
echo Random Number: %result%
echo.
echo Press 'Y' to close press 'N' to go to menu...
echo.
set/p input= "Exit: "
if %input%==y exit
if %input%==n goto loop
这就是我要找的。谢谢@gaborosz您应该在括号内添加
+1
,否则将永远不会生成上限(模运算符生成的结果小于其第二个参数)。尝试使用bottomlimit=1
和upperlimit=2
来清楚地看到问题。@LorenzoDonati你是对的!它产生除法的剩余部分,当然总是小于除法。很抱歉…记住,这只是一个丑陋的例子!如果在其上形成函数,请执行边界检查(bottomlimit
小于upperlimit
),或计算除法器的绝对值以允许负限制。此外,如果您的范围大于%RANDOM%
功能,则需要乘法或使用多个%RANDOM%
来扩展边界…+1,但使用SET/A时不需要扩展变量。但是,由于随机变量是一个虚拟动态变量,因此必须对其进行扩展。您可以简单地使用set/a result=bottomLimit+%random%%%(upperLimit-bottomLimit+1)
@NabeelSaad,您发布的是一个bash shell脚本。OP询问了CMD批处理,这相当清楚地表明他正在使用Windows。这是对一个问题的误导性回答。
:loop
cls
goto before
:before
@echo off
setlocal delayexpansion
cls
@mod con: clos=80 lines=25
title Number Picker
color 0a
cls
goto menu
:menu
cls
echo.
echo Number Picker
echo.
set/p input= "Number: "
if %input%== goto show
:show
set /a bottomlimit = 0
set /a upperlimit = %input%
set /a result = %bottomlimit% + %random% %% (%upperlimit% - %bottomlimit% +1)
echo.
echo Random Number: %result%
echo.
echo Press 'Y' to close press 'N' to go to menu...
echo.
set/p input= "Exit: "
if %input%==y exit
if %input%==n goto loop