Different random numbers at the same time

I have a VBScript that looks like

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)

Now several instances of this script are working at the same time, so they will probably all be seeded at the same time. As a result, they all give the same random number at a time. Is there a way to get them to give different random values ​​at the same time?

+4
source share
2 answers

I had some success with this, although I don’t know how you use the scripts that I reproduced using 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

Launch this way

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

sample output

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

+4

Randomize ( PID . -).

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
...
+2

Source: https://habr.com/ru/post/1682290/


All Articles