Jump to content

Infinite Loop should exit (I think) but doesn't


frew
 Share

Recommended Posts

Hello,

I'm wondering why this gets stuck in an infinite loop.

The message box keeps popping up with the number one, even though it seems like this code is telling it to subtract 1 from 1 at a certain point and then exit.

$i = Random(1, 50, 1)


While 1

    MsgBox(0, Default, $i)

    If $i >= 1 Then
        $i = $i - Random(1, $i, 1);shouldn't this return zero soon?
    EndIf

    MsgBox(0, Default, $i)


    If $i = 0 Then
        ExitLoop
    EndIf


WEnd

Thanks for any ideas about this.

frew

Edit: Please try it a few times, because it exits as expected sometimes, then with further tests it keeps getting stuck at 1.

Edited by frew
Link to comment
Share on other sites

You have said to the Random function, the min it can return is 1 - thus it is always going to return between a range of 1 and $i.

I ran it and it works ok for me - 3 times.

What is it you are trying to do?

Edited by bo8ster

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

So what happen is, when $i = 1 and Random returns 0 you have 1 - 0 = 1.

Valuater for some reason Random does return 0.

This is my code that I tested it on

#AutoIt3Wrapper_Run_Debug_Mode=Y

$i = Random(1, 50, 1)

While 1
Local $y
    MsgBox(0, Default, $i)

    If $i >= 1 Then
        $y = Random(1, $i, 1);shouldn't this return zero soon?
        ConsoleWrite("random returned " & $y & @CRLF)
        $i = $i - $y 
        ConsoleWrite("I is now " & $i & @CRLF)
    EndIf

    MsgBox(0, Default, $i)


    If $i = 0 Then
        ExitLoop
    EndIf

WEnd

There are times when $y = 0

Here was the output

CODE
!> Starting in DebugMode..

Line: @error-@extended: Line syntax

0001: 0-0: #AutoIt3Wrapper_Run_Debug_Mode=Y

0003: 0-0: $i = Random(1, 50, 1)

0005: 0-0: While 1

0006: 0-0: Local $y

0007: 0-0: MsgBox(0, Default, $i)

0009: 0-0: If $i >= 1 Then

0010: 0-0: $y = Random(1, $i, 1);shouldn"t this return zero soon?

0011: 0-0: ConsoleWrite("random returned " & $y & @CRLF)

random returned 9

0012: 0-0: $i = $i - $y

0013: 0-0: ConsoleWrite("I is now " & $i & @CRLF)

I is now 7

0014: 0-0: EndIf

0016: 0-0: MsgBox(0, Default, $i)

0019: 0-0: If $i = 0 Then

0024: 0-0: WEnd

0006: 0-0: Local $y

0007: 0-0: MsgBox(0, Default, $i)

0009: 0-0: If $i >= 1 Then

0010: 0-0: $y = Random(1, $i, 1);shouldn"t this return zero soon?

0011: 0-0: ConsoleWrite("random returned " & $y & @CRLF)

random returned 3

0012: 0-0: $i = $i - $y

0013: 0-0: ConsoleWrite("I is now " & $i & @CRLF)

I is now 4

0014: 0-0: EndIf

0016: 0-0: MsgBox(0, Default, $i)

0019: 0-0: If $i = 0 Then

0024: 0-0: WEnd

0006: 0-0: Local $y

0007: 0-0: MsgBox(0, Default, $i)

0009: 0-0: If $i >= 1 Then

0010: 0-0: $y = Random(1, $i, 1);shouldn"t this return zero soon?

0011: 0-0: ConsoleWrite("random returned " & $y & @CRLF)

random returned 1

0012: 0-0: $i = $i - $y

0013: 0-0: ConsoleWrite("I is now " & $i & @CRLF)

I is now 3

0014: 0-0: EndIf

0016: 0-0: MsgBox(0, Default, $i)

0019: 0-0: If $i = 0 Then

0024: 0-0: WEnd

0006: 0-0: Local $y

0007: 0-0: MsgBox(0, Default, $i)

0009: 0-0: If $i >= 1 Then

0010: 0-0: $y = Random(1, $i, 1);shouldn"t this return zero soon?

0011: 0-0: ConsoleWrite("random returned " & $y & @CRLF)

random returned 2

0012: 0-0: $i = $i - $y

0013: 0-0: ConsoleWrite("I is now " & $i & @CRLF)

I is now 1

0014: 0-0: EndIf

0016: 0-0: MsgBox(0, Default, $i)

0019: 0-0: If $i = 0 Then

0024: 0-0: WEnd

0006: 0-0: Local $y

0007: 0-0: MsgBox(0, Default, $i)

0009: 0-0: If $i >= 1 Then

0010: 0-0: $y = Random(1, $i, 1);shouldn"t this return zero soon?

0011: 1-0: ConsoleWrite("random returned " & $y & @CRLF)

random returned 0

0012: 0-0: $i = $i - $y

0013: 0-0: ConsoleWrite("I is now " & $i & @CRLF)

I is now 1

0014: 0-0: EndIf

0016: 0-0: MsgBox(0, Default, $i)

0019: 0-0: If $i = 0 Then

0024: 0-0: WEnd

0006: 0-0: Local $y

0007: 0-0: MsgBox(0, Default, $i)

0009: 0-0: If $i >= 1 Then

0010: 0-0: $y = Random(1, $i, 1);shouldn"t this return zero soon?

0011: 1-0: ConsoleWrite("random returned " & $y & @CRLF)

random returned 0

0012: 0-0: $i = $i - $y

0013: 0-0: ConsoleWrite("I is now " & $i & @CRLF)

I is now 1

0014: 0-0: EndIf

0016: 0-0: MsgBox(0, Default, $i)

0019: 0-0: If $i = 0 Then

0024: 0-0: WEnd

0006: 0-0: Local $y

0007: 0-0: MsgBox(0, Default, $i)

0009: 0-0: If $i >= 1 Then

0010: 0-0: $y = Random(1, $i, 1);shouldn"t this return zero soon?

0011: 1-0: ConsoleWrite("random returned " & $y & @CRLF)

random returned 0

0012: 0-0: $i = $i - $y

0013: 0-0: ConsoleWrite("I is now " & $i & @CRLF)

I is now 1

0014: 0-0: EndIf

0016: 0-0: MsgBox(0, Default, $i)

0019: 0-0: If $i = 0 Then

0024: 0-0: WEnd

0006: 0-0: Local $y

0007: 0-0: MsgBox(0, Default, $i)

+>09:30:31 AutoIT3.exe ended.rc:0

Edited by bo8ster

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

Thanks for that.

Doc update - the devs love that. Should that be a trac item or what?

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

This also comes down to good practice of checking @error. I would also expect the same thing if the min is greater then the max.

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

Thanks for the replies and ideas. I'm trying to keep up with you guys.

Please try this one:

$i = 1

While 1

    MsgBox(0, Default, $i)

    If $i >= 1 Then
        $i = $i - Random(1, $i, 1)
    EndIf

    MsgBox(0, Default, $i)


    If $i = 0 Then
        ExitLoop
    EndIf


WEnd

On my end this causes the message box to go into an infinite loop, stuck at 1.

Shouldn't $i = $i - Random(1, $i, 1) assign $i the value of 0, since $i starts out as 1?

Doesn't the above line state that 1 - 1 (ie since Random(1, 1 ,1) must be 1) is the new value for $i?

So $i = 1 - 1 is basically the case here, since in this script $i starts off as 1.

But it turns out that the message box for this script gets stuck in an infinite loop at 1 here.

...I wonder why that is?

Thanks for any other ideas.

frew

Link to comment
Share on other sites

Thanks Authenticity, but what I mean is that

since $i = 1 in this script

then it seems that

$i = $i - Random(1, $i, 1)

should mean

the value of $i is assigned 1 - 1

so

the value of $i should now be 0.

Thanks for any other ideas.

frew

Link to comment
Share on other sites

This is what is happening in your script

When i$ = 1 and you do Random(1, $i, 1) Random returns 0.

So then $i = $i - Random(1, $i, 1) really is $i = 1 - 0. And since you just assigned $i to 1 again the same thing happens over and over again. $i = 1 -0, and $i never becomes 0.

And so i'm clear - as Val pointed out, Random(1, 1 ,1) returns 0. It should return 1 but because the min and max are the same, an error occurs and 0 is returned not 1 as expected.

Edited by bo8ster

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

bo8ster I thank you very much for clarifying that for me. Yes that makes sense to me now.

By the way, here's a little workaround for a little thing I'm doing that may work for now.

Edit: sorry, this is not the code I meant to put here

$i = Random(1, 49, 1)


While 1

    MsgBox(0, Default, $i)

    If $i = 1 Then
    Exit;or do other stuff
    EndIf
    
    MsgBox(0, Default, $i)

    If $i <= 49 Then
        $i = $i - Random(1, $i, 1)
    EndIf

    MsgBox(0, Default, $i)
    
    If $i = 1 Then
    Exit;or do other stuff
    EndIf


    If $i = 0 Then
        ExitLoop
    EndIf



WEnd

It's just a little text editing script that goes down a list of 49 lines at random increments until it reaches the bottom of the list, but attempts to go no further than the bottom of the list...and each time it goes through the list it picks a different set of lines to edit. It could be two lines, five lines, ten, etc...each time through a different set of lines is picked and edited, but it always stops at the bottom line...not going on further to mess up any of the following lines.

...well, it doesn't quite work yet, because it goes past line 49 and messed up lines below that...so I'll work on it.

Thank so much for your help and ideas.

frew

Edit: I meant to put this: It attempts to comment out some lines at random, for creative purposes

$i = Random(1, 49, 1)

While 1

    MsgBox(0, Default, $i)
    
    Send("{Down " & $i & "}")
    Sleep(500)
    Send(";")
    Sleep(500)
    Send("{Left}")
    Sleep(1000) 

    If $i = 1 Then
    Exit;or do other stuff
        
    MsgBox(0, Default, $i)

    ElseIf $i <= 49 Then
        $i = $i - Random(1, $i, 1)
    EndIf

    MsgBox(0, Default, $i)
    
    If $i = 1 Then
    Exit;or do other stuff
    EndIf


    If $i = 0 Then
        ExitLoop
    EndIf

But it still goes past line 49, so I work on it.

Thanks, Seeya,

frew

Edited by frew
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...