About now I know what you’re thinking. Not another VBScript program! But yes, another one :-D This one is actually setup for a future thing I’m currently working on (yeah, mixed tenses baby!). What this does is simulate the results of tossing 1,000 coins 100 times. It creates pseudorandom data (although I should point out that it’s pretty much impossible to distinguish between pseudorandom and actual random data), which is what I’ll be using it for.
In any case, here’s the code:
' coinflip.vbs
Option Explicit
Const xlFormat = -4143
Dim intHighNumber, intLowNumber, intCount, intNum, strOutput
Dim numHigh, numLow, outerLoop
Dim fso, outFileName, outFilePath, excel, dataSheet, intRow
' notify script is working....
wscript.Echo "Click OK to begin."
' set up file
Set fso = CreateObject("Scripting.FileSystemObject")
outFilePath = fso.GetAbsolutePathName("")
outFileName = "\coin.xls"
outFileName = outFilePath & outFileName
set excel = CreateObject("Excel.Application")
excel.DisplayAlerts = 0
If (Err.Number <> 0) Then
On Error Goto 0
MsgBox("Excel application not found...quiting")
wscript.Quit
End If
On Error Goto 0
excel.Workbooks.Add
set DataSheet = excel.ActiveWorkbook.Worksheets(1)
dataSheet.Name = "Output"
dataSheet.Cells(1,1).Value = "Num 0"
dataSheet.Cells(1,2).Value = "Num 1"
dataSheet.Cells(1,3).value = "Difference"
' set up random variables
intHighNumber = 1
intLowNumber = 0
intRow = 2
For OuterLoop = 1 to 100
For intCount = 1 to 1000
Randomize
intNum = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
strOutput = strOutput & intNum & " "
if intNum = 0 then
numLow = numLow + 1
else
numHigh = numHigh + 1
End if
Next
' Output to file
dataSheet.Cells(intRow,1).value = numLow
dataSheet.Cells(intRow,2).value = numHigh
dataSheet.Cells(intRow,3).Value = abs(numLow - numHigh)
intRow = intRow + 1
numLow = 0
numHigh = 0
Next
dataSheet.Cells(intRow,1).Value = "AVERAGE DIFF"
dataSheet.Cells(intRow,3).Value = "=Average(C2:C" & intRow - 1 & ")"
excel.ActiveWorkbook.SaveAs outFileName, xlFormat
excel.ActiveWorkbook.Close
excel.Quit
wscript.Echo "Done."
And here’s the data I got for one trial run (formatted with number of 0s, number of 1s—the 0s can be heads, the 1s tails or vice verse, depending on how you feel—and the difference between the number of occurrences):
Num 0 Num 1 Difference 516 484 32 520 480 40 502 498 4 482 518 36 501 499 2 487 513 26 504 496 8 509 491 18 526 474 52 501 499 2 520 480 40 498 502 4 487 513 26 530 470 60 504 496 8 449 551 102 471 529 58 531 469 62 513 487 26 558 442 116 548 452 96 541 459 82 478 522 44 489 511 22 536 464 72 467 533 66 535 465 70 472 528 56 532 468 64 515 485 30 527 473 54 498 502 4 545 455 90 505 495 10 526 474 52 454 546 92 530 470 60 447 553 106 490 510 20 525 475 50 498 502 4 512 488 24 498 502 4 502 498 4 483 517 34 485 515 30 507 493 14 484 516 32 493 507 14 505 495 10 479 521 42 486 514 28 491 509 18 500 500 0 530 470 60 499 501 2 479 521 42 518 482 36 488 512 24 493 507 14 495 505 10 487 513 26 502 498 4 497 503 6 498 502 4 500 500 0 509 491 18 500 500 0 492 508 16 499 501 2 490 510 20 523 477 46 510 490 20 488 512 24 503 497 6 525 475 50 491 509 18 498 502 4 497 503 6 492 508 16 507 493 14 494 506 12 503 497 6 526 474 52 486 514 28 502 498 4 513 487 26 551 449 102 539 461 78 509 491 18 535 465 70 492 508 16 506 494 12 482 518 36 514 486 28 507 493 14 495 505 10 546 454 92 507 493 14 503 497 6
The average difference between the “heads” and “tails” column for this data run: 32.32
BTW, you can see a nifty graph of the differences here (although it unfortunately went in reverse order):

I’ve run it a few times and it tends to be in the 20 range for the averages; but of course since it’s random data, your results will vary!
Don’t worry, I will explain more of why I am doing this later on. For now, time for me to get to work.
