我有一个看起来像这样的VBScript
Randomize
Dim oAPI, oBag
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()
Call oBag.AddValue("Random Performance", Int(Rnd*100)+1)
Call oAPI.Return(oBag)
现在,此脚本的多个实例在同一时间运行,因此它们可能同时都被播种。结果,它们一次都给出相同的随机数。是否有可能让它们同时给出不同的随机值?
答案 0 :(得分:4)
我在这方面取得了一些成功,虽然我不知道你是如何运行脚本我使用start /b
' create a number from the Guid
dim g
g = Mid(CreateObject("Scriptlet.TypeLib").Guid, 2, 36)
dim i, c, s
s = 0
for i = 1 to len(g)
c = Mid(g, i, 1)
if not IsNumeric(c) then
c = Asc(c)
end if
s = CInt(c) + s
next
randomize s
for i = 0 to 5
WScript.Echo(WScript.Arguments(0) & " " & g & " " & CInt(Rnd*100))
next
这样运行它
start /b cscript test.vbs //nologo 1
start /b cscript test.vbs //nologo 2
start /b cscript test.vbs //nologo 3
start /b cscript test.vbs //nologo 4
start /b cscript test.vbs //nologo 5
示例输出
2D1F39B7-6158-4C77-893B-5C749CF5537F 32 1
2D1F39B7-6158-4C77-893B-5C749CF5537F 13 1
2D1F39B7-6158-4C77-893B-5C749CF5537F 49 1
2D1F39B7-6158-4C77-893B-5C749CF5537F 51 1
2D1F39B7-6158-4C77-893B-5C749CF5537F 84 1
2D1F39B7-6158-4C77-893B-5C749CF5537F 59 3
DC29EDBD-9C63-495B-AFB6-A4A18F65C7F8 35 3
DC29EDBD-9C63-495B-AFB6-A4A18F65C7F8 86 3
DC29EDBD-9C63-495B-AFB6-A4A18F65C7F8 80 3
DC29EDBD-9C63-495B-AFB6-A4A18F65C7F8 3 3
DC29EDBD-9C63-495B-AFB6-A4A18F65C7F8 90 3
DC29EDBD-9C63-495B-AFB6-A4A18F65C7F8 49 4
DC7D2269-436F-4053-B008-F9375BA50F30 34 4
DC7D2269-436F-4053-B008-F9375BA50F30 88 4
DC7D2269-436F-4053-B008-F9375BA50F30 85 4
DC7D2269-436F-4053-B008-F9375BA50F30 60 4
DC7D2269-436F-4053-B008-F9375BA50F30 5 4
DC7D2269-436F-4053-B008-F9375BA50F30 52 2
22F2D6A9-CBF4-48D5-919B-05798FC1BFB1 7 2
22F2D6A9-CBF4-48D5-919B-05798FC1BFB1 89 2
22F2D6A9-CBF4-48D5-919B-05798FC1BFB1 14 2
22F2D6A9-CBF4-48D5-919B-05798FC1BFB1 68 2
22F2D6A9-CBF4-48D5-919B-05798FC1BFB1 89 2
22F2D6A9-CBF4-48D5-919B-05798FC1BFB1 73 5
B13E0D1A-1F93-4387-8CFE-70EEF7FB9B4F 60 5
B13E0D1A-1F93-4387-8CFE-70EEF7FB9B4F 71 5
B13E0D1A-1F93-4387-8CFE-70EEF7FB9B4F 48 5
B13E0D1A-1F93-4387-8CFE-70EEF7FB9B4F 31 5
B13E0D1A-1F93-4387-8CFE-70EEF7FB9B4F 3 5
B13E0D1A-1F93-4387-8CFE-70EEF7FB9B4F 19 5
答案 1 :(得分:2)
我会使用进程ID作为Randomize
的种子(用于确定PID,例如来自Kul-Tigin的this answer)。
Function GetMyPID
ppid = 0
Set sh = CreateObject("WScript.Shell")
Set wmi = GetObject("winmgmts://./root/cimv2")
cmd = "/k " & Mid(CreateObject("Scriptlet.TypeLib").Guid, 2, 36)
sh.Run "%comspec% " & cmd, 0, False
WScript.Sleep 100
qry = "SELECT * FROM Win32_Process WHERE CommandLine LIKE '%" & cmd & "'"
For Each p In wmi.ExecQuery(qry)
ppid = p.ParentProcessId
p.Terminate
Exit For
Next
GetMyPID = ppid
End Function
Randomize GetMyPID
...