Random numbers, letters, pictures?

P

Preschool Mike

I have four questions about generating items randomly.

I know how to generate numbers randomly, but how do I generate

1. Numbers less than 5 but greater than -1?
2. Two digit numbers.

Also is it possible to generate random letters in a shape or text box and
random pictures?

I'm currently using the code below to generate my random numbers.
Dim First As Integer
Dim Second As Integer
Dim Answer As Integer


Sub Initalize()
Randomize
ActivePresentation.SlideShowWindow.View.Next
End Sub

Sub RandomNumber()

First = CStr(Int(Rnd() * 10))
Second = CStr(Int(Rnd() * 10))
ActivePresentation.Slides(2).Shapes("Box1").TextFrame.TextRange.Text = First
ActivePresentation.Slides(2).Shapes("Box2").TextFrame.TextRange.Text = Second
End Sub

Sub InsertAnswer()
Answer = InputBox("Type in the answer")
If Answer = First + Second Then
ActivePresentation.Slides(2).Shapes("Box3").TextFrame.TextRange.Text = Answer
MsgBox ("That's the correct answer")
Else
MsgBox ("That's not correct, try again")
End If
End Sub
 
M

Mark

I have four questions about generating items randomly.

I know how to generate numbers randomly, but how do I generate

1. Numbers less than 5 but greater than -1?
2. Two digit numbers.

Also is it possible to generate random letters in a shape or text box and
random pictures?

I'm currently using the code below to generate my random numbers.
Dim First As Integer
Dim Second As Integer
Dim Answer As Integer

Sub Initalize()
Randomize
ActivePresentation.SlideShowWindow.View.Next
End Sub

Sub RandomNumber()

First = CStr(Int(Rnd() * 10))
Second = CStr(Int(Rnd() * 10))
ActivePresentation.Slides(2).Shapes("Box1").TextFrame.TextRange.Text = First
ActivePresentation.Slides(2).Shapes("Box2").TextFrame.TextRange.Text = Second
End Sub

Sub InsertAnswer()
Answer = InputBox("Type in the answer")
If Answer = First + Second Then
ActivePresentation.Slides(2).Shapes("Box3").TextFrame.TextRange.Text = Answer
MsgBox ("That's the correct answer")
Else
MsgBox ("That's not correct, try again")
End If
End Sub

Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

So, if you want a number between 10 and 60:

Int((60- 10+ 1) * Rnd + 10)


For random letters, you could generate a number from 65 to 90 (for
capital letters), and use the Chr(charcode) function to convert to a
letter.
 
D

David Marcovitz

Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

So, if you want a number between 10 and 60:

Int((60- 10+ 1) * Rnd + 10)


For random letters, you could generate a number from 65 to 90 (for
capital letters), and use the Chr(charcode) function to convert to a
letter.

Mark is right on target. As for random pictures, that is a bit harder.
Where are the pictures stored? Are they already on the slide, and you
just want to show a random one, or are they in a file, and you need to
insert them? If they are in a file, the easiest thing might be to name
the files with consecutive numbers (MyPic1.jpg, MyPic2.jpg,
MyPic3.jpg...) and use your random number generator to figure out which
number to append to the file name in your insert procedure. If they are
on the slide and just need to be shown, you could do a Case..Select
statement (for some reason, I can never remember the exact format of
this off the top of my head) or a complex If statement. Just generate a
number and then

If myRandomNumber = 1 Then
<insert code to show the first picture>
ElseIf myRandomNumber = 2 Then
<insert code to show the second picture>
ElseIf my RandomNumber = 3 Then
<insert code to show the third picture>
....
End If

--David

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

Preschool Mike

Thanks for your help Mark. I'm a not sure how to use the Chr(charcode)
function. Could you give me an example of how I would write the procedure?
 
P

Preschool Mike

Thanks for your help David. Actually I haven't even started anything with
the random pictures yet. I was only wondering if it could be done. Your
example gives me a good place to start from when I do begin.
 
M

Mark

Thanks for your help Mark.  I'm a not sure how to use the Chr(charcode)
function.  Could you give me an example of how I would write the procedure?
--
Mike Mast
Special Education Preschool Teacher









- Show quoted text -

Chr(charcode) returns the character that is represented by the given
charcode. Look up Chr in the help section for more info.

Example:
Chr(65) will give you the capital "A"
Chr(97) will give you the lowercase "a"

characters 65-90 are 'A-Z'; characters 97-122 are 'a-z'
 
D

David Marcovitz

Chr(charcode) returns the character that is represented by the given
charcode. Look up Chr in the help section for more info.

Example:
Chr(65) will give you the capital "A"
Chr(97) will give you the lowercase "a"

characters 65-90 are 'A-Z'; characters 97-122 are 'a-z'

To take this one more step for Mike:

MsgBox "Hello"

puts up a box with Hello on the screen.

MsgBox Chr(72) & "ello"

also puts up Hello because Chr(72) is H.

One more step is then:

Msgbox Chr(myRandomNum) & "ello"

Puts up a random letter (assuming you used the previous code to create a
random number from 65-90) followed by the letters ello.

--David

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

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