3

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.

CptGoodar
  • 303
  • 2
  • 15
  • I'm sorry but I can't resist: what has the question been **taking** to be "wired"? – QHarr Jan 16 '18 at 09:07
  • haha, many many hours of my life on a simply rng – CptGoodar Jan 16 '18 at 09:08
  • Is this working as a recursive sub? – QHarr Jan 16 '18 at 09:17
  • I haven't checked this fully but can't you simplify a lot of that nested If code to something like: https://pastebin.com/sRw6wMDz – QHarr Jan 16 '18 at 09:28
  • I get totally different values for the random variables. How many iterations have you tried? – Vegard Jan 16 '18 at 09:29
  • @QHarr i'm sure you can as you've showed this just turned out like this but slowly adding bits to it. I shall try your code. Thanks – CptGoodar Jan 16 '18 at 09:34
  • @Vegard I have tried running the sub 10x by itself and then it gives totally random number, but whenever the sub is run within the main code I always get 79 and 6 and that was tried 10x as well – CptGoodar Jan 16 '18 at 09:35
  • @QHarr i've just tried your code and found that its still gives me 79 and 6. Ran it 5 times – CptGoodar Jan 16 '18 at 09:37
  • My code was meant as a simplification of what you have already written not a solution. Sorry for any confusion :-) – QHarr Jan 16 '18 at 09:39
  • @QHarr ahh sorry that's my fault but your code is far neater looking. – CptGoodar Jan 16 '18 at 09:42
  • Can you also post the part of the code that you use to determine which values you've gotten for the random values? Also, in `Data_Setup` sub, it seems like you're going to overwrite the limits using an inputbox regardless of variable status. And what happens in `Limit_Def`? – Vegard Jan 16 '18 at 09:42
  • Actually - can you try this: https://stackoverflow.com/a/26281164/4604845 – Vegard Jan 16 '18 at 09:43
  • @Vegard in `Data_Setup` the input boxes should only be called if the user wants to entre the values thereself. And should be missed if they want randomly generated solutions. – CptGoodar Jan 16 '18 at 09:44
  • @Vegard i've just posted Limit_Def in the main question – CptGoodar Jan 16 '18 at 09:44
  • @Vegard I shall try this and see what i get – CptGoodar Jan 16 '18 at 09:46
  • @Vegard that has worked. over 5x run through. But that would fail to explain why it generated random numbers when runnig the code by itself. Dont worry about answering that I'm just curious – CptGoodar Jan 16 '18 at 09:48

1 Answers1

2

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.

Vegard
  • 3,587
  • 2
  • 22
  • 40