random number generation in a word table

J

J Storey

I am trying to randomly number the cells in a 5 x 5 table
from 1 to 25.
can anyone help?
 
P

Peter Hewett

Hi J Storey

You can use this code:

Public Sub FillTablewithRandomNumbers()
Const cRandMin As Integer = 1
Const cRandMax As Integer = 25
Const cRandHighest As Integer = cRandMax - cRandMin + 1

Dim celItem As Word.Cell

' Seed random number generator
Randomize

' Fill table with random number in the range 1-25, table can be ANY size
Set celItem = ActiveDocument.Tables(1).Range.Cells(1)
Do Until celItem Is Nothing
celItem.Range.Text = Int(cRandHighest * Rnd + cRandMin)

Set celItem = celItem.Next
Loop
End Sub

It fills the first table in the document. It does not check that the document
contains at least 1 table. The table can actually be of any size. It fills
each cell with a random number in the range 1-25 (you can change this by
editing the Const statements). The random numbers are not unique.

HTH + Cheers - Peter
 
J

Jay Freedman

Hi, Peter,

Your macro will fill the table with random numbers from 1 to 25, but
some of them may be duplicates while other numbers in the range may
not occur in the table. Instead, I got the impression that the request
was to put in each number from 1 to 25, but in random order. Here's a
macro to do that.

Sub RandomTable()
Dim RandArray(1 To 25) As Integer
Dim Idx As Integer, Idx2 As Integer
Dim Temp As Integer
Dim oTbl As Table

' check for existence of table
' with minimum size 5x5
If ActiveDocument.Tables.Count = 0 Then
' no table, so make one
Set oTbl = ActiveDocument.Tables.Add( _
Range:=Selection.Range, _
numrows:=5, numcolumns:=5)
Else
Set oTbl = ActiveDocument.Tables(1)
If (oTbl.Rows.Count < 5) Or _
(oTbl.Columns.Count < 5) Then
' too small -- complain and quit
MsgBox "Expected table to be at least 5x5."
Exit Sub
End If
End If

' start with an in-order list of numbers
For Idx = 1 To 25
RandArray(Idx) = Idx
Next Idx

' initialize the rnd generator
Randomize

' swap each entry with a
' randomly chosen other entry
For Idx = 1 To 25
Idx2 = Int(25 * Rnd + 1)
Temp = RandArray(Idx)
RandArray(Idx) = RandArray(Idx2)
RandArray(Idx2) = Temp
Next Idx

' put the entries into the table cells
' left to right, then down
For Idx = 1 To 5
For Idx2 = 1 To 5
oTbl.Cell(Row:=Idx, Column:=Idx2).Range.Text = _
RandArray(5 * (Idx - 1) + Idx2)
Next Idx2
Next Idx
End Sub
 
P

Peter Hewett

Hi Jay

I stated in my post "The random numbers are not unique", and since
uniqueness was not explicitly requested I didn't bother to implement card
shuffling.

Peter
 
J

Jay Freedman

Hi, Peter,

No criticism intended. :) I just read a different meaning into the original
post, which could have been clearer. Anyway, now the OP can pick either way.
 
P

Peter Hewett

Hi Jay

Sorry for that I was far to damn sensitive (it's my New Age thing comming out
late!)

Cheers - Peter
 

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