Generate 12 random numbers


P

Preschool Mike

Is it possible to generate 12 random numbers (numbers 1 - 12) independently
(no numbers repeated) in 12 independent shapes (one number per shape) by the
click of a single button? I can come up with the code to generate 12 random
numbers, but with my code there's always the chance that some of the numbers
will be the same (repeated). I want them all to be different and from 1 to
12. Here's a shortened example of what I'm using.

Dim First As Integer
Dim Second As Integer
Dim Third As Integer

Sub Initalize()
Randomize
First = 0
Second = 0
Third = 0
ActivePresentation.SlideShowWindow.View.Next
End Sub

Sub RandomNumber()
First = Int((12 - 1 + 1) * Rnd + 1)
Second = Int((12 - 1 + 1) * Rnd + 1)
Third = Int((12 - 1 + 1) * Rnd + 1)
ActivePresentation.Slides(2).Shapes("Box1").TextFrame.TextRange.Text = First
ActivePresentation.Slides(2).Shapes("Box2").TextFrame.TextRange.Text = Second
ActivePresentation.Slides(2).Shapes("Box3").TextFrame.TextRange.Text = Third
End Sub


Thanks,
 
Ad

Advertisements

M

Mark

Is it possible to generate 12 random numbers (numbers 1 - 12) independently
(no numbers repeated) in 12 independent shapes (one number per shape) by the
click of a single button?  I can come up with the code to generate 12 random
numbers, but with my code there's always the chance that some of the numbers
will be the same (repeated).  I want them all to be different and from 1 to
12.  Here's a shortened example of what I'm using.

Dim First As Integer
Dim Second As Integer
Dim Third As Integer

Sub Initalize()
Randomize
First = 0
Second = 0
Third = 0
ActivePresentation.SlideShowWindow.View.Next
End Sub

Sub RandomNumber()
First = Int((12 - 1 + 1) * Rnd + 1)
Second = Int((12 - 1 + 1) * Rnd + 1)
Third = Int((12 - 1 + 1) * Rnd + 1)
ActivePresentation.Slides(2).Shapes("Box1").TextFrame.TextRange.Text = First
ActivePresentation.Slides(2).Shapes("Box2").TextFrame.TextRange.Text = Second
ActivePresentation.Slides(2).Shapes("Box3").TextFrame.TextRange.Text = Third
End Sub

Thanks,

The best way I know to do this would be to create an array of numbers
from 1 to 12 (or any other values you want to use), and then use a
function to randomize that array. Here's a link to code to do that:

http://www.freevbcode.com/ShowCode.asp?ID=7399
 
M

Mark

The best way I know to do this would be to create an array of numbers
from 1 to 12 (or any other values you want to use), and then use a
function to randomize that array. Here's a link to code to do that:

http://www.freevbcode.com/ShowCode.asp?ID=7399- Hide quoted text -

- Show quoted text -

Oops, sorry, I believe the link I supplied is for VB, not VBA. But the
idea is the same; just find a link to code for VBA to shufffle an
array.
 
P

Preschool Mike

Thanks for your help. I went to the site you suggested, but to be honest
it's all a bit above my skill level. I don't really know where to begin.
How do I create an array of numbers? and then how do I plug that into the
code on the link you suggested? How would I display the results?
 
D

David Marcovitz

Is it possible to generate 12 random numbers (numbers 1 - 12) independently
(no numbers repeated) in 12 independent shapes (one number per shape) by the
click of a single button? I can come up with the code to generate 12 random
numbers, but with my code there's always the chance that some of the numbers
will be the same (repeated). I want them all to be different and from 1 to
12. Here's a shortened example of what I'm using.

Dim First As Integer
Dim Second As Integer
Dim Third As Integer

Sub Initalize()
Randomize
First = 0
Second = 0
Third = 0
ActivePresentation.SlideShowWindow.View.Next
End Sub

Sub RandomNumber()
First = Int((12 - 1 + 1) * Rnd + 1)
Second = Int((12 - 1 + 1) * Rnd + 1)
Third = Int((12 - 1 + 1) * Rnd + 1)
ActivePresentation.Slides(2).Shapes("Box1").TextFrame.TextRange.Text = First
ActivePresentation.Slides(2).Shapes("Box2").TextFrame.TextRange.Text = Second
ActivePresentation.Slides(2).Shapes("Box3").TextFrame.TextRange.Text = Third
End Sub


Thanks,

Quick and dirty method
Dim done as Boolean
First = Int((12 - 1 + 1) * Rnd + 1)
Second = Int((12 - 1 + 1) * Rnd + 1)
done = False
While done = False
If Second = First Then
done = False
Else
done = True
End If
Wend
Third = Int((12 - 1 + 1) * Rnd + 1)
done = False
While done = False
If Third = First Or Third = Second Then
done = False
Else
done = True
End If
Wend
Fourth = Int((12 - 1 + 1) * Rnd + 1)
done = False
While done = False
If Fourth = First Or Fourth = Second Or Fourth = Third Then
done = False
Else
done = True
End If
Wend

....

I'm sure there is a better way (and this could take a while as it has to
loop through every time it chooses a number that was already chosen),
but it will work (although I haven't tested it; this is just air code).

--David

--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 
M

Mark

Thanks for your help.  I went to the site you suggested, but to be honest
it's all a bit above my skill level.  I don't really know where to begin.  
How do I create an array of numbers? and then how do I plug that into the
code on the link you suggested?  How would I display the results?
--
Mike Mast
Special Education Preschool Teacher







- Show quoted text -

Try this. Check the function 'testArray' for an example on how to use
the code.

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ShuffleArrayInPlace
' This shuffles InArray to random order, randomized in place.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim N As Long
Dim L As Long
Dim Temp As Variant
Dim J As Long

Randomize
L = UBound(InArray) - LBound(InArray) + 1
For N = LBound(InArray) To UBound(InArray)
J = Int((UBound(InArray) - LBound(InArray) + 1) * Rnd +
LBound(InArray))
If N <> J Then
Temp = InArray(N)
InArray(N) = InArray(J)
InArray(J) = Temp
End If
Next N
End Function

Private Function testArray()
Dim x() ' Define x as an array
x = Array(1, 2, 3, 4, 5) ' Populate array with numbers from 1 to 5
ShuffleArrayInPlace x() ' Shuffle array

Debug.Print "First number is " & x(0)
Debug.Print "Second number is " & x(1)
Debug.Print "Third number is " & x(2)
Debug.Print "Fourth number is " & x(3)
Debug.Print "Fifth number is " & x(4)
End Function
 
Ad

Advertisements

M

Mark

Try this. Check the function 'testArray' for an example on how to use
the code.

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''­'''''''''
' ShuffleArrayInPlace
' This shuffles InArray to random order, randomized in place.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''­'''''''''
    Dim N As Long
    Dim L As Long
    Dim Temp As Variant
    Dim J As Long

    Randomize
    L = UBound(InArray) - LBound(InArray) + 1
    For N = LBound(InArray) To UBound(InArray)
        J = Int((UBound(InArray) - LBound(InArray) + 1) * Rnd +
LBound(InArray))
        If N <> J Then
            Temp = InArray(N)
            InArray(N) = InArray(J)
            InArray(J) = Temp
        End If
    Next N
End Function

Private Function testArray()
    Dim x() ' Define x as an array
    x = Array(1, 2, 3, 4, 5) ' Populate array with numbers from 1 to 5
    ShuffleArrayInPlace x() ' Shuffle array

    Debug.Print "First number is " & x(0)
    Debug.Print "Second number is " & x(1)
    Debug.Print "Third number is " & x(2)
    Debug.Print "Fourth number is " & x(3)
    Debug.Print "Fifth number is " & x(4)
End Function- Hide quoted text -

- Show quoted text -

Sorry, the function definition for ShuffleArrayInPlace got cut off
when I copied in my previous post.

Private Function ShuffleArrayInPlace(InArray() As Variant)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ShuffleArrayInPlace
' This shuffles InArray to random order, randomized in place.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim N As Long
Dim L As Long
Dim Temp As Variant
Dim J As Long

Randomize
L = UBound(InArray) - LBound(InArray) + 1
For N = LBound(InArray) To UBound(InArray)
J = Int((UBound(InArray) - LBound(InArray) + 1) * Rnd +
LBound(InArray))
If N <> J Then
Temp = InArray(N)
InArray(N) = InArray(J)
InArray(J) = Temp
End If
Next N
End Function

Private Function testArray1()
Dim x() ' Define x as an array
x = Array(1, 2, 3, 4, 5) ' Populate array with numbers from 1 to 5
ShuffleArrayInPlace x() ' Shuffle array

Debug.Print "First number is " & x(0)
Debug.Print "Second number is " & x(1)
Debug.Print "Third number is " & x(2)
Debug.Print "Fourth number is " & x(3)
Debug.Print "Fifth number is " & x(4)
End Function
 
J

John Wilson

Here's one way

Sub randnos()
Dim raynum(1 To 12)
Dim i As Integer
Dim Num_a As Integer
Dim Num_b As Integer
Dim Num_c As Integer
Dim Num_d As Integer
Dim stRresult As String
'fill the array
For i = 1 To 12
raynum(i) = i
Next i
'shuffle
For i = 1 To 20
Randomize
Num_a = Int((12 * Rnd) + 1)
Num_b = Int((12 * Rnd) + 1)
Num_c = raynum(Num_a)
Num_d = raynum(Num_b)
raynum(Num_b) = Num_c
raynum(Num_a) = Num_d
Next i
For i = 1 To 12
stRresult = stRresult & raynum(i) & "/ "
Next i
MsgBox stRresult
End Sub

--
john ATSIGN PPTAlchemy.co.uk

Free PPT Hints, Tips and Tutorials
http://www.pptalchemy.co.uk/powerpoint_hints_and_tips_tutorials.html
 
P

Preschool Mike

John this works nicely in the msgbox and I can even get the results to
display in one shape, but how would I get each number to display in 12
different shapes/textboxes?
 
P

Preschool Mike

David thanks for your help. I've tried the code but ocassionally I get hung
up in the loop. Maybe I'm missing something. Here's what I'm working with.

Dim First As Integer
Dim Second As Integer
Dim Third As Integer
Dim Fourth As Integer

Sub RandomNotSame()
'Quick and dirty method
Dim done As Boolean
First = Int((12 - 1 + 1) * Rnd + 1)
Second = Int((12 - 1 + 1) * Rnd + 1)
done = False
While done = False
If Second = First Then
done = False
Else
done = True
End If
Wend
Third = Int((12 - 1 + 1) * Rnd + 1)
done = False
While done = False
If Third = First Or Third = Second Then
done = False
Else
done = True
End If
Wend
Fourth = Int((12 - 1 + 1) * Rnd + 1)
done = False
While done = False
If Fourth = First Or Fourth = Second Or Fourth = Third Then
done = False
Else
done = True
End If
Wend
ActivePresentation.Slides(5).Shapes("Box1").TextFrame.TextRange.Text = First
ActivePresentation.Slides(5).Shapes("Box2").TextFrame.TextRange.Text = Second
ActivePresentation.Slides(5).Shapes("Box3").TextFrame.TextRange.Text = Third
ActivePresentation.Slides(5).Shapes("Box4").TextFrame.TextRange.Text = Fourth
End Sub
 
D

David Marcovitz

Just replace:

For i = 1 To 12
stRresult = stRresult & raynum(i) & "/ "
Next i
MsgBox stRresult

with something like:

First = raynum(1)
Second = raynum(2)
....

Or, just access raynum(1) and raynum(2) directly. For example,

ActivePresenation.Slides(3).Shapes(4).TextFrame.TextRange.Text = raynum(7)

(watch out for linebreaks inserted by the news system) will put the 7th
item into shape 4 on slide 3 (assuming shape 4 on slide 3 is a text box).

--David

John this works nicely in the msgbox and I can even get the results to
display in one shape, but how would I get each number to display in 12
different shapes/textboxes?


--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 
Ad

Advertisements

D

David Marcovitz

The problem with my code is that if the random number generator takes a
while to get to the open slot (constantly picking numbers that have come
before), it can get hung up for a while (though it should eventually
end). I think other code, like John's, will work better for you. Just
remember that, instead of using First, Second, Third, and Fourth, you
can use raynum(1), raynum(2), raynum(3), and raynum(4).
--David

David thanks for your help. I've tried the code but ocassionally I get hung
up in the loop. Maybe I'm missing something. Here's what I'm working with.

Dim First As Integer
Dim Second As Integer
Dim Third As Integer
Dim Fourth As Integer

Sub RandomNotSame()
'Quick and dirty method
Dim done As Boolean
First = Int((12 - 1 + 1) * Rnd + 1)
Second = Int((12 - 1 + 1) * Rnd + 1)
done = False
While done = False
If Second = First Then
done = False
Else
done = True
End If
Wend
Third = Int((12 - 1 + 1) * Rnd + 1)
done = False
While done = False
If Third = First Or Third = Second Then
done = False
Else
done = True
End If
Wend
Fourth = Int((12 - 1 + 1) * Rnd + 1)
done = False
While done = False
If Fourth = First Or Fourth = Second Or Fourth = Third Then
done = False
Else
done = True
End If
Wend
ActivePresentation.Slides(5).Shapes("Box1").TextFrame.TextRange.Text = First
ActivePresentation.Slides(5).Shapes("Box2").TextFrame.TextRange.Text = Second
ActivePresentation.Slides(5).Shapes("Box3").TextFrame.TextRange.Text = Third
ActivePresentation.Slides(5).Shapes("Box4").TextFrame.TextRange.Text = Fourth
End Sub


--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 
M

Mark

John this works nicely in the msgbox and I can even get the results to
display in one shape, but how would I get each number to display in 12
different shapes/textboxes?
--
Mike Mast
Special Education Preschool Teacher








- Show quoted text -

The code which John and I posted uses an array. To access an
individual item, you get an item from the Array using it's index
number (which is 0-based, i.e. the first item is numbered 0)

raynum(i)

where i is the item index. So raynum(4) will give you the fifth item
of the array.

textBox.value = raynum(4)

will put the 5th item in the array into the value for textBox.
 
Ad

Advertisements


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