Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for rndcomp.vbs

(view source code of rndcomp.vbs as plain text)

  1. Option Explicit
  2.  
  3. ' This should work ok
  4. TestRandomComp 10
  5.  
  6. ' This should generate an error
  7. TestRandomComp  0
  8.  
  9.  
  10. Sub TestRandomComp( myReq )
  11. ' This is a test subroutine for my RANDOM.WSC component
  12.  
  13. 	Dim arrResult, i, intAvg, objFSO, objRnd, strWSC
  14.  
  15. 	On Error Resume Next
  16. 	' First let's see if the component is registered
  17. 	Set objRnd = CreateObject( "robvanderwoude.Random" )
  18. 	If Err Then
  19. 		' If not, check if it exists in the current directory
  20. 		' and use an alternative method to reference the component
  21. 		Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  22. 		With objFSO
  23. 			strWSC = .BuildPath( .GetParentFolderName( WScript.ScriptFullName ), "Random.wsc" )
  24. 			If .FileExists( strWSC ) Then
  25. 				strWSC = "script:" & strWSC
  26. 				Set objRnd = GetObject( strWSC )
  27. 			Else
  28. 				WScript.Echo "Random.wsc not registered, " _
  29. 				           & "nor found in current directory"
  30. 			End If
  31. 		End With
  32. 	Set objFSO = Nothing
  33. 	End If
  34. 	On Error Goto 0
  35.  
  36. 	' Abort if we couldn't instantiate the object
  37. 	If Not IsObject( objRnd ) Then WScript.Quit 1
  38.  
  39. 	' Set the required properties and query random.org
  40. 	objRnd.LowerLimit  = 1
  41. 	objRnd.UpperLimit  = 6
  42. 	objRnd.NumRequests = myReq
  43. 	objRnd.Query
  44.  
  45. 	WScript.Echo vbCrLf _
  46. 	           & "Random.wsc, version " & objRnd.Version    & vbCrLf _
  47. 	           & "Lower limit = "       & objRnd.LowerLimit & vbCrLf _
  48. 	           & "Upper limit = "       & objRnd.UpperLimit & vbCrLf _
  49. 	           & "NumRequests = "       & myReq             & vbCrLf
  50.  
  51. 	If objRnd.Error Then
  52. 		WScript.Echo vbCrLf _
  53. 		           & "An error occurred, check the Debugging Info!" & vbCrLf
  54. 	Else
  55. 		WScript.Echo vbCrLf & "Results:" & vbCrLf & "========" & vbCrLf
  56. 		arrResult = objRnd.Result
  57. 		intAvg = 0
  58. 		For i = 0 To objRnd.NumRequests - 1
  59. 			WScript.Echo "Result[" & i & "] = " & arrResult(i)
  60. 			intAvg = intAvg + arrResult(i)
  61. 		Next
  62. 		WScript.Echo "Average   = " & ( intAvg / 10 )
  63. 	End If
  64.  
  65. 	WScript.Echo vbCrLf & vbCrLf & "Debugging Info:" & vbCrLf & "==============="
  66. 	WScript.Echo objRnd.Debug & vbCrLf
  67.  
  68. 	Set objRnd = Nothing
  69. End Sub
  70.  

page last uploaded: 2017-07-06, 12:37