Home » excel » excel – Why does this only generate 1 pair of solutions

excel – Why does this only generate 1 pair of solutions

Posted by: admin April 23, 2020 Leave a comment

Questions:

This is a slightly wired question.

I have a long 1500 line code with multiple subs within, the idea behind is smoothing 100,000’s data points.

I have a random number generator within the code, found below but whenever I run the main code my two Limits are always UpperAngleLimit= 79 and LowerAngleLimit = 6 but whenever I run only this sub I find that I get totaly random number like planned.

Below is the code for the Random Limits generateion.

Sub Random_Limits()

UpperAngleLimit = Int((90 - 1 + 1) * Rnd + 1)
LowerAngleLimit = Int((90 - 1 + 1) * Rnd + 1)




If UpperAngleLimit = LastUpperLimit Then

    Call Random_Limits

Else

    If LowerAngleLimit = LastLowerLimit Then

        Call Random_Limits

    Else

        If UpperAngleLimit > LowerAngleLimit Then

            If UpperAngleLimit > 60 Then

                If LowerAngleLimit < 45 Then

                    LastLowerLimit = LowerAngleLimit
                    LastUpperLimit = UpperAngleLimit

                    Call Calculate_Angle

                Else

                    Call Random_Limits

                End If

            Else

                Call Random_Limits

            End If

        Else

            Call Random_Limits

        End If

    End If

End If

End Sub

I’ve have also placed all other mentions of either UpperAngleLimit & LowerAngleLimit as well.

Global UpperAngleLimit As Double
Global LowerAngleLimit As Double

^^Variable definitaions

RandomLimits = MsgBox("Would you like to have random limits generated?", vbYesNo)

If RandomLimits = vbYes Then

    Call Random_Limits

End If

UpperAngleLimit = InputBox("What Upper Angle Limit would you like to spline the curve from?")
LowerAngleLimit = InputBox("What Lower Angle Limit would you like to spline the curve from?")

Call Limit_Def

^^ Used if the user dosen’t want to generate the limits randomly

If Cells(j, 3) < UpperAngleLimit And Cells(j, 3) > LowerAngleLimit Then

^^ Used to identify indiviual cells

Cells(1, 4) = "Curve Data produced for limits at " & UpperAngleLimit & " and " & LowerAngleLimit & " @ " & Limit

^^ Used to name data group

These are all the mentions of the both UpperAngleLimit and LowerAngleLimit

I relise that I may be hard for you to get the same issue as I have with them always being 79 & 6 but you may be able to see a glearing error that I have overlooked.

Some extra detail,

The code will always produce 79 and 6 on its 3rd loop with the same number being produced beforehand.

Run 1 – UpperAngleLimit = 64 LowerAngleLimit = 49
Run 2 – UpperAngleLimit = 74 LowerAngleLimit = 64
Run 3 – UpperAngleLimit = 78 lowerAngleLimit = 72
Run 4 – UpperAngleLimit = 79 LowerAngleLimit = 6

This was confirmed through 5 run throughs

I have no attached the sub that call Random_Limits to provide furhter detail

Sub Data_SetUp()

Application.ScreenUpdating = False

Sheets("Sheet1").Columns(2).Copy Destination:=Sheets("Sheet2").Columns(1)
Sheets("Sheet1").Columns(5).Copy Destination:=Sheets("Sheet2").Columns(2)

Worksheets("Sheet2").Activate
Rows(4).EntireRow.Delete

Cells(3, 1) = "Time"
Cells(3, 2) = "Throttle"
Cells(3, 3) = "Angle"
Cells(2, 1).Select
Selection.ClearContents


StartTimer = Timer


Iterations = InputBox("How many iteration would you like to run?")
IterationNumber = 1

RandomLimits = MsgBox("Would you like to have random limits generated?", vbYesNo)

If RandomLimits = vbYes Then

    Call Random_Limits

End If

UpperAngleLimit = InputBox("What Upper Angle Limit would you like to spline the curve from?")
LowerAngleLimit = InputBox("What Lower Angle Limit would you like to spline the curve from?")

Call Limit_Def

End Sub

On request here is Limit_Def

Sub Limit_Def()

LimitUpdate = MsgBox("Would you like to keep the distance limit the same throughout?", vbYesNo)

If LimitUpdate = vbYes Then

    LimitNow = MsgBox("Would you like to choose your limit now?", vbYesNo)

        If LimitNow = vbYes Then

                Limit = InputBox("Please set a line distance limit")

        Else

        End If
End If



Call Calculate_Angle

End Sub

Thank you for any help you can provide.

How to&Answers:

As per this answer: If you have problems with Rnd, use Randomize beforehand to re-seed the random number generator.

Syntax

Randomize [ number ]

The optional number argument is a Variant or any valid numeric
expression.

Remarks
Randomize uses number to initialize the Rnd
function’s random-number generator, giving it a new seed value. If you
omit number, the value returned by the system timer is used as the new
seed value. If Randomize is not used, the Rnd function (with no
arguments) uses the same number as a seed the first time it is called,
and thereafter uses the last generated number as a seed value.

Why Rnd works by itself in some cases and doesn’t work by itself in other cases is a bit beyond me, but I would venture to guess that it’s related to the main sub holding control over the stack or the run-time scope, preventing Rnd from randomizing itself (as it is supposed to) – essentially creating the situation described at the end of the above quote. Maybe.