CMD-批处理文件简单随机

时间:2013-08-21 23:37:53

标签: batch-file range

使用.bat文件,我知道如何使用%Random%回显一个随机数。如何设置%Random%的某个随机范围,例如50-100?哦,我有一个在开始时说明的点系统: SET / A MAINSCORE = 0 如何设置随机范围编号以从MainScore中添加/减去? 感谢。

4 个答案:

答案 0 :(得分:7)

%RANDOM%返回一个介于0和32767之间的数字。要缩小这些范围,请使用模运算符和加法或减法来抵消结果。例如:

@set /a bottomlimit = 50
@set /a upperlimit = 100
@set /a result = %bottomlimit% + %RANDOM% %% (%upperlimit% - %bottomlimit% + 1)
@echo %result%

答案 1 :(得分:1)

@echo off
set min=5
set max=10
set /a range=max-min +1
set /a rnd=%random% %%%range% +%min%
echo %rnd%
pause
个人我真的很喜欢我这样做的方式 这将通过5到10之间的任何# 最重要的是,它干净。谢谢

答案 2 :(得分:0)

这应该有所帮助:

# 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"

从这里采取:http://tldp.org/LDP/abs/html/randomvar.html

然后,一旦你有了$ number,你就可以这样做:     NUM = expr $number + $MAINSCORE

希望有所帮助。

答案 3 :(得分:0)

简单的程序,允许您设置简单选择程序的数字量...

let dateString = "2014-08-21T19:32:00Z"

// iOS 10 or later
let dateFormatter = ISO8601DateFormatter()

// Older deployment targets
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ"


if let jsonDate = dateFormatter.date(from: dateString) {

    let today = Date()

    // Number of seconds between the 2
    let difference = today.timeIntervalSince(jsonDate)

    let componentsFormatter = DateComponentsFormatter()
    componentsFormatter.unitsStyle = .short
    componentsFormatter.allowedUnits = [.year, .month, .day, .hour, .minute, .second, .nanosecond]
    let string = componentsFormatter.string(from: jsonDate, to: today)
    print(string!)


    let comparison = Calendar.autoupdatingCurrent.dateComponents([.year, .month, .day, .hour, .minute, .second, .nanosecond], from: jsonDate, to: today)

    print(comparison)
}