Throwing two dice and print out the total of each throw

B

Bob

in the days of line numbers i build a short programme to simulate the
throwing of two dice with a screen print out the number of times, a
particular number randomly came up . My question is can this run using Access
VB ? Any constructive advise would be most welcome . The old code is as
follows:
REM THROWING 2 DICE
CLS
INPUT "How many Throws", N
Dim A(12)
FOR M = 1 TO 12
A(M) = 0
Next M
FOR T = 1 TO N
X = int(6 * rnd (1)) + 1
Y = int(6 * rnd (1)) + 1
Z = X + Y
A (Z) = A(Z)+1
NEXT T
CLS
PRINT TAB(5); "Total" ; TAB(25); " Frequency"
PRINT
FOR M = 2 TO 12
PRINT TAB(7) ; M; TAB(28) ;A(M)
NEXT M
END
 
A

Albert D. Kallal

Yes, you can...

The only decision here is for your output window.

You can use the debug/development window, or you can use a form With a text
box that will scroll as a window.

Lets first do this with the debug window (no form needed, just code).

Option Compare Database

Sub Test1()

Rem THROWING 2 DICE
n = InputBox("How many Throws")

Dim A(12)
For M = 1 To 12
A(M) = 0
Next M
For T = 1 To n
X = Int(6 * Rnd(1)) + 1
y = Int(6 * Rnd(1)) + 1
Z = X + y
A(Z) = A(Z) + 1
Next T

Debug.Print Tab(5); "Total"; Tab(25); " Frequency"
Debug.Print
For M = 2 To 12
Debug.Print Tab(7); M; Tab(28); A(M)
Next M

End Sub

Like when useing te PET, or TRS80, or the Apple II, There is often some
minor differences in the code syntax. For example, we don't have a CLS
command. and, we had to change the print command to debug.print.

The output for entering a value of 2 is:

Total Frequency

2 0
3 0
4 0
5 0
6 0
7 1
8 0
9 1
10 0
11 0
12 0


A surprising amount of the old style basic programming syntax actually works
inside of the MS access, even the code to read and write text files remains
unchanged as it existined in the old DOS basic system from 22 years ago.

For example:

Sub ReadTextFile

Dim strFile As String
Dim intF As Integer
Dim strLineBuf As String
Dim lngLines As Long
Dim lngBlank As Long

strFile = "c:\my data\MyData.txt"

intF = FreeFile()
Open strFile For Input As #intF

Do While EOF(intF) = False
Line Input #intF, strLineBuf
If Trim(strLineBuf) = "" Then
lngBlank = lngBlank + 1
Else
lngLines = lngLines + 1
End If
Loop
Close intF

End If

MsgBox "Number non blank lines = " & lngLines & vbCrLf & _
"Blank lines = " & lngBlank & vbCrLf & _
"Total = " & lngBlank + lngLines

End Function

If you look at the use of the input# command, it's exactly the same syntax
that we had twenty years ago. About the only difference in the above is that
I use a message box command in place of a (debug) print command and
therefore the user sees the answer displayed in a dialog box in windows on
the screen, instead of on the output text in the debug window..
 
A

Albert D. Kallal

Sorry, one more thing....


To the run the code, either put your cursor in the code and hit f5, or from
the debug window simply type in the name of hte sub.


eg:

Test1

Also, do save the code module. Note that the code module name MUST be
different then the sub name in side. You can put lots of subs inside of one
code module....
 
A

Albert D. Kallal

As a side note, I think I forgot the original instructions or step by step
how to get into the code editor....

This step(s) might not be clear.

The first thing we have to do is to create a standard code module in which
you can enter some of your code routines. Keep in mind that each code module
you create can have quite a few different subroutines, or little sets of
code that you want to run. Often what happens is if I have some routines to
read and write files, then I will create a code module for all that code
that reads and writes files.

Here's a typical screen shot of all the modules I created in a typical
access application:

http://www.members.shaw.ca/AlbertKallal/test/index2.htm

So in the above screen shot, each module created can have quite a bit of
code in it. The conceptual idea here is that you create modules to allow you
to Group like minded code into the same code module.

The above screen shot is access 2003, and you can see I have models for word
code, testing, etc...

So before you can type in some code, the first thing you do is select the
modules tab, then create a new code module. You can then type in the code as
per my other message.

As mentioned each code module you create can have more than one subroutine
or code "function".
 
S

Steve

If you do a little research in Statistics, you will be able to calculate the
probability of any number between 2 and 12 coming up when throwing of two
dice. This will be more accurate than your simulation.

Steve
(e-mail address removed)
 
J

James A. Fortune

If you do a little research in Statistics, you will be able to calculate the
probability of any number between 2 and 12 coming up when throwing of two
dice. This will be more accurate than your simulation.

That's a great point Steve. BTW, Merry Christmas. Here's a codeless
version I tried:

Using two queries:

tblIntegers
ID AutoNumber
theInt Long
ID theInt
1 1
2 2
3 3
....
25 25

qryTheCrosstab:
TRANSFORM Count(qryRolls.Total) AS CountOfTotal
SELECT qryRolls.theInt
FROM (SELECT 20 As NumRolls, theInt, ID, Rnd(ID) * 10^9 AS Seed, SEED
Mod 6 + 1 As Pos, Pos + Pos As Total FROM tblIntegers WHERE
tblIntegers.theInt <= 20) AS qryRolls
GROUP BY qryRolls.theInt
PIVOT qryRolls.Total IN (2,3,4,5,6,7,8,9,10,11,12);

qrySummary:
SELECT Sum(Nz(qryTheCrosstab.[2], 0)) AS 2s, Sum(Nz(qryTheCrosstab.
[3], 0)) AS 3s, Sum(Nz(qryTheCrosstab.[4], 0)) AS 4s, Sum(Nz
(qryTheCrosstab.[5], 0)) AS 5s, Sum(Nz(qryTheCrosstab.[6], 0)) AS 6s,
Sum(Nz(qryTheCrosstab.[7], 0)) AS 7s, Sum(Nz(qryTheCrosstab.[8], 0))
AS 8s, Sum(Nz(qryTheCrosstab.[9], 0)) AS 9s, Sum(Nz(qryTheCrosstab.
[10], 0)) AS 10s, Sum(Nz(qryTheCrosstab.[11], 0)) AS 11s, Sum(Nz
(qryTheCrosstab.[12], 0)) AS 12s
FROM qryTheCrosstab;

!qrySummary
2s 3s 4s 5s 6s 7s 8s 9s 10s 11s 12s
1 0 3 4 2 4 3 1 0 0 2

A97 version of qryTheCrosstab:

TRANSFORM Count(qryRolls.Total) AS CountOfTotal
SELECT qryRolls.theInt
FROM [SELECT 20 As NumRolls, theInt, ID, Rnd(ID) * 10^9 AS Seed, SEED
Mod 6 + 1 As Pos, Pos + Pos As Total FROM tblIntegers WHERE
tblIntegers.theInt <= 20 ]. AS qryRolls
GROUP BY qryRolls.theInt
PIVOT qryRolls.Total IN (2,3,4,5,6,7,8,9,10,11,12);

Note that the number of rolls must be changed near the <=. The other
20 is for information only. All of the above can probably be
simplified.

James A. Fortune
(e-mail address removed)
 
S

Steve

Merry Christmas to you and your family too, Jim. I wish you too a Happy,
Healthy and Prosperpous New Year.

Steve


James A. Fortune said:
If you do a little research in Statistics, you will be able to calculate
the
probability of any number between 2 and 12 coming up when throwing of two
dice. This will be more accurate than your simulation.

That's a great point Steve. BTW, Merry Christmas. Here's a codeless
version I tried:

Using two queries:

tblIntegers
ID AutoNumber
theInt Long
ID theInt
1 1
2 2
3 3
...
25 25

qryTheCrosstab:
TRANSFORM Count(qryRolls.Total) AS CountOfTotal
SELECT qryRolls.theInt
FROM (SELECT 20 As NumRolls, theInt, ID, Rnd(ID) * 10^9 AS Seed, SEED
Mod 6 + 1 As Pos, Pos + Pos As Total FROM tblIntegers WHERE
tblIntegers.theInt <= 20) AS qryRolls
GROUP BY qryRolls.theInt
PIVOT qryRolls.Total IN (2,3,4,5,6,7,8,9,10,11,12);

qrySummary:
SELECT Sum(Nz(qryTheCrosstab.[2], 0)) AS 2s, Sum(Nz(qryTheCrosstab.
[3], 0)) AS 3s, Sum(Nz(qryTheCrosstab.[4], 0)) AS 4s, Sum(Nz
(qryTheCrosstab.[5], 0)) AS 5s, Sum(Nz(qryTheCrosstab.[6], 0)) AS 6s,
Sum(Nz(qryTheCrosstab.[7], 0)) AS 7s, Sum(Nz(qryTheCrosstab.[8], 0))
AS 8s, Sum(Nz(qryTheCrosstab.[9], 0)) AS 9s, Sum(Nz(qryTheCrosstab.
[10], 0)) AS 10s, Sum(Nz(qryTheCrosstab.[11], 0)) AS 11s, Sum(Nz
(qryTheCrosstab.[12], 0)) AS 12s
FROM qryTheCrosstab;

!qrySummary
2s 3s 4s 5s 6s 7s 8s 9s 10s 11s 12s
1 0 3 4 2 4 3 1 0 0 2

A97 version of qryTheCrosstab:

TRANSFORM Count(qryRolls.Total) AS CountOfTotal
SELECT qryRolls.theInt
FROM [SELECT 20 As NumRolls, theInt, ID, Rnd(ID) * 10^9 AS Seed, SEED
Mod 6 + 1 As Pos, Pos + Pos As Total FROM tblIntegers WHERE
tblIntegers.theInt <= 20 ]. AS qryRolls
GROUP BY qryRolls.theInt
PIVOT qryRolls.Total IN (2,3,4,5,6,7,8,9,10,11,12);

Note that the number of rolls must be changed near the <=. The other
20 is for information only. All of the above can probably be
simplified.

James A. Fortune
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top