• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

[MOD PLEASE MOVE TO PROGRAMMING] Macros help: random odd number generator

KingstonU

Golden Member
Hope this is the right forum and I think I almost have the code right. I only need a slight modification to it.

Trying to create random flash cards using power point.


Slide 1 is question. Slide 2 is Answer. All odd numbered slides are questions and their subsequent answer is the next slide.

After seeing an answer (even numbered slide) I would place a link that runs the macros which picks the next random question (odd numbered slide).

So I would just need a macros that chooses another random odd numbered slide. That number should also be less then the total number of slides.



I think I almost have it working from an example I found that is loosely similar but it doesn't quite work perfectly. This is what I have so far:

Dim visited() As Boolean
Dim numSlides As Long
Dim numRead As Integer
Dim numWanted As Integer
Sub Initialize()
Randomize
numWanted = 5
numRead = 0
numSlides = ActivePresentation.Slides.Count
ReDim visited(numSlides)
For i = 2 To numSlides - 1
visited(i) = False
Next i
End Sub
Sub RandomNext()
Dim nextSlide As Long

If numRead >= numWanted Or numRead >= numSlides - 2 Then
ActivePresentation.SlideShowWindow.View.Last
Else
nextSlide = Int((numSlides - 2) * Rnd + 2)
While visited(nextSlide) = True
nextSlide = Int((numSlides - 2) * Rnd + 2)
Wend
ActivePresentation.SlideShowWindow.View.GotoSlide nextSlide
End If
End Sub


Anyone know what I need to change or have a better code for this?

Thank you very much.
 
Last edited:
What is the purpose of numWanted being 5?

You have a sub called Initialize but it is never called.

I see a lot of offsetting by two and subtracting by one. Try to avoid that sort of game playing by simply treating the slides as pairs, and use your > < = appropriately so you do not have to add/subtract 1.

Look at this line:

While visited(nextSlide) = True

Is it possible that nextSlide could be uninitialized when that loop is first encountered? It looks like that wont be a problem for your code, but it is a practice you'll want to avoid.

Have you tested the output of Rnd to see if it is what you think it is? I would write a very simple macro that calls Rnd a bunch of times and prints the results. Usually you seed a random number generator, but I'm not seeing that code.
 
Back
Top