New to Vbscript.
I made a random function with two parameters.And called it
It resulted in a infinite loop opening infinite program.
Function random(v1,v2)
Randomize
rdm =(Int((v2 - v1 + 1)* Rnd + v1))
End Function
Function download()
Set shell = createobject("wscript.shell"):shell.run "mspaint.exe"
End function
'I want this download function to run rarely
Do
Call random(100,1000)
If rdm>700 And rdm <760 Then
Call download()
End If
loop
Added code to return
value and Added Exit Do
statement to break the loop.
Hope this will help you..
Function random(v1,v2)
Randomize
random=(Int((v2 - v1 + 1)* Rnd + v1))
End Function
Function download()
Set shell = createobject("wscript.shell"):shell.run "mspaint.exe"
download=true
End function
'I want this download function to run rarely
Do
rdm= random(100,1000)
If rdm>700 And rdm <760 Then
Call download()
Exit Do ' this will break the loop if condition is met
End If
loop
If your intention is to write a script which, when called, invokes a function with probability 60/900 = 1/15, you are making things too complicated. There is no reason to generate a random integer in the range 100 to 1000 just to check if it lies in a certain range. If you want to trigger an action with probability p
, have a line which looks like:
If rnd() < p Then 'action
If I understand your intentions, your entire script could be written as:
Randomize
Set shell = createobject("wscript.shell")
If rnd() < 1/15 Then shell.Run "mspaint.exe"
Most of the time you run the above script nothing happens, but an average of 1/15 times it will open paint.