Vbscript 很少执行函数,有机会使用随机

Vbscript execute a function rarely, with chances using random

Vbscript 新手。 我用两个 parameters.And 调用了一个随机函数 导致无限循环打开无限程序

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

return 值添加了代码并添加了 Exit Do 语句来中断循环。

希望这对您有所帮助..

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

如果您打算编写一个脚本,在调用时以 60/900 = 1/15 的概率调用一个函数,那么您就把事情搞得太复杂了。没有理由生成 100 到 1000 范围内的随机整数只是为了检查它是否位于某个范围内。如果你想触发一个概率为 p 的动作,有一行看起来像:

If rnd() < p Then 'action

如果我理解你的意图,你的整个脚本可以写成:

Randomize

Set shell = createobject("wscript.shell")   

If rnd() < 1/15 Then shell.Run "mspaint.exe"

大多数时候你运行上面的脚本没有任何反应,但平均有1/15次它会打开paint。