If I put nine pieces of paper, with a digit from 1-9 written on each one, into a hat, and pull 3 out without replacement, what is the likelihood that I will select three numbers that result in a winning tic-tac-toe?
1 | 2 | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
The odds of winning are between .094 and .095 out of 1.0
Here is the Visual Basic program I used to swat this fly:
Imports System
Imports System.IO
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Math
Module Module1
Sub Main()
Randomize()
Dim strgoagain As String
Dim intwinning(8, 3) As Integer
intwinning(0, 0) = 1
intwinning(0, 1) = 2
intwinning(0, 2) = 3
intwinning(1, 0) = 4
intwinning(1, 1) = 5
intwinning(1, 2) = 6
intwinning(2, 0) = 7
intwinning(2, 1) = 8
intwinning(2, 2) = 9
intwinning(3, 0) = 1
intwinning(3, 1) = 4
intwinning(3, 2) = 7
intwinning(4, 0) = 2
intwinning(4, 1) = 5
intwinning(4, 2) = 8
intwinning(5, 0) = 3
intwinning(5, 1) = 6
intwinning(5, 2) = 9
intwinning(6, 0) = 1
intwinning(6, 1) = 5
intwinning(6, 2) = 9
intwinning(7, 0) = 3
intwinning(7, 1) = 5
intwinning(7, 2) = 7
strgoagain = "?"
While strgoagain <> "N"
mainline(intwinning)
Console.WriteLine("Again ? (Y/N)")
strgoagain = UCase(Console.ReadLine())
End While
End Sub
Sub mainline(ByRef intwinning)
Dim dblwins As Double
Dim dblloop As Double
Dim dblgamessofar As Double
Dim intcounter As Integer
dblwins = 0
dblgamessofar = 0
dblloop = 0
intcounter = 0
While dblloop <= 0
Console.WriteLine("How many times shall we play?")
dblloop = Int(Console.ReadLine())
End While
For index1 As Double = 0 To dblloop
playthegame(intwinning, dblwins, dblgamessofar)
intcounter += 1
If intcounter > 9999 Then
intcounter = 0
Console.WriteLine(Str(dblgamessofar) & _
" games so far...")
Console.WriteLine("Winning odds so far are " & _
Str(dblwins / dblgamessofar))
End If
Next
Console.WriteLine(" ")
Console.WriteLine("Odds of winning are " & _
Str(dblwins / dblloop))
End Sub
Sub playthegame(ByRef intwinning, ByRef dblwins, _
ByRef dblgamessofar)
Dim intwork1(9) As Integer
Dim intgame(3) As Integer
Dim intrandom1 As Integer
Dim intrandom2 As Integer
dblgamessofar += 1
intwork1(0) = 1
intwork1(1) = 2
intwork1(2) = 3
intwork1(3) = 4
intwork1(4) = 5
intwork1(5) = 6
intwork1(6) = 7
intwork1(7) = 8
intwork1(8) = 9
intrandom1 = 8
For index1 As Integer = 0 To 2
intrandom2 = Int((intrandom1 - 0 + 1) * Rnd()) + 0
intrandom1 -= 1
intgame(index1) = intwork1(intrandom2)
intwork1(intrandom2) = 0
If intrandom2 < 9 Then
For index2 As Integer = intrandom2 To 8
intwork1(index2) = intwork1(index2 + 1)
intwork1(index2 + 1) = 0
Next
End If
Next
For index1 As Integer = 0 To 7
If intwinning(index1, 0) = intgame(0) And _
intwinning(index1, 1) = intgame(1) And _
intwinning(index1, 2) = intgame(2) Then
dblwins += 1
Exit Sub
End If
If intwinning(index1, 0) = intgame(0) And _
intwinning(index1, 1) = intgame(2) And _
intwinning(index1, 2) = intgame(1) Then
dblwins += 1
Exit Sub
End If
If intwinning(index1, 0) = intgame(1) And _
intwinning(index1, 1) = intgame(0) And _
intwinning(index1, 2) = intgame(2) Then
dblwins += 1
Exit Sub
End If
If intwinning(index1, 0) = intgame(1) And _
intwinning(index1, 1) = intgame(2) And _
intwinning(index1, 2) = intgame(0) Then
dblwins += 1
Exit Sub
End If
If intwinning(index1, 0) = intgame(2) And _
intwinning(index1, 1) = intgame(0) And _
intwinning(index1, 2) = intgame(1) Then
dblwins += 1
Exit Sub
End If
If intwinning(index1, 0) = intgame(2) And _
intwinning(index1, 1) = intgame(1) And _
intwinning(index1, 2) = intgame(0) Then
dblwins += 1
Exit Sub
End If
Next
End Sub
End Module
|
Posted by Penny
on 2004-07-30 18:05:43 |