Search code examples
loopsrandomvbscriptcall

Vbscript execute a function rarely, with chances using random


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

Solution

  • 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