Two numbers are different but treated as equal. What the heck?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Two numbers are different but treated as equal. What the heck?

08 Oct 2020, 15:48

I generally avoid to create 2 thread per day, but it is too interesting why the following code doesn't work as I expect it.

(And yes, we don't need two separate functions here, it could be done much simpler. It is just an example.)

Code: Select all

Customers := ["Bill", "Karl", "Tom"]

FindLuckyCustomer(Customers)
{
    for Index, Customer in Customers
        if (IsLucky(Customer) = Index)
            return "Lucky:" A_Tab IsLucky(Customer) "`n"
                 . "Index:" A_Tab Index "`n"
                 .  Customer

    return ""
}

IsLucky(Customer)
{
    Global Customers
    NumberOfCustomers := Customers.Length()
    Random, LuckyNumber, 1, % NumberOfCustomers
    return LuckyNumber
}

msgbox % FindLuckyCustomer(Customers)
Sometimes I get results like this one:

Code: Select all

Lucky: 2
Index: 1
Bill
Why? I mean, it is expected that if numbers are not equal, the return must be the empty string! For example:

Code: Select all

Lucky: 2
Index: 2
Karl
User avatar
mikeyww
Posts: 26889
Joined: 09 Sep 2014, 18:38

Re: Two numbers are different but treated as equal. What the heck?

08 Oct 2020, 16:07

I noticed that your second function does not actually use its parameter anywhere in the function. That is unusual-- probably the error at hand.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Two numbers are different but treated as equal. What the heck?

08 Oct 2020, 16:14

You are calling IsLucky(Customer) twice causing a missmatch in the returned output.

HTH
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Two numbers are different but treated as equal. What the heck?

08 Oct 2020, 16:48

@Xtra Could you show where exactly? I really don't see. I don't think you mean return.

Code: Select all

            return "Lucky:" A_Tab IsLucky(Customer) "`n"
                 . "Index:" A_Tab Index "`n"
                 .  Customer
@mikeyww Yes, exactly. A small typo.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Two numbers are different but treated as equal. What the heck?

08 Oct 2020, 17:03

1) if (IsLucky(Customer) = Index)
2) return "Lucky:" A_Tab IsLucky(Customer) "`n"
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Two numbers are different but treated as equal. What the heck?

08 Oct 2020, 17:07

You realize that each time you call IsLucky(Customer), it is giving a potentially different result. So when you get a number returned from IsLucky in this statement:

Code: Select all

if (IsLucky(Customer) = Index)
...it will often be a different value then you get in the return statement:

Code: Select all

return "Lucky:" A_Tab IsLucky(Customer) "`n"
In fact, you're also grabbing a new lucky number each time through, so it's not fair because you return before the others get a chance at being lucky. Bill would be lucky more often than the others, and Karl more often than Tom. You should be picking the lucky number once, then see which one it matches so it's not biased based on order.

Edit: Fixed typo
Last edited by boiler on 08 Oct 2020, 17:16, edited 1 time in total.
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Two numbers are different but treated as equal. What the heck?

08 Oct 2020, 17:14

This is a fair draw and more succinct (and isn't subject to the previous problems):

Code: Select all

Customers := ["Bill", "Karl", "Tom"]

FindLuckyCustomer(Customers)
{
	LuckyNumber := IsLucky(Customers)
	return "Lucky:" A_Tab LuckyNumber "`n"
                 .  Customers[LuckyNumber]
}

IsLucky(Customers)
{
    Random, LuckyNumber, 1, Customers.Length()
    return LuckyNumber
}

msgbox % FindLuckyCustomer(Customers)
By the way, you don't need to make Customers global in the second function because you passed it as a parameter.
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Two numbers are different but treated as equal. What the heck?

08 Oct 2020, 17:31

To explain why the originally posted approach is unfair (once the typos/bugs are worked out of it):

Bill has a 33% chance of being lucky. The other 67% of the time, it will drop down to Karl, who has a chance to get lucky. So Karl will get lucky 33% of the 67% of the time it fell to him, or 22% of the time overall. Then 67% of 67% of the time (which is 44% of the time overall), it will drop down to Tom, who has a 33% chance of getting lucky when it happens to reach him. Overall he gets lucky 33% of the 44% of the time it reaches him, which is 15% of the time.

So Bill gets lucky 33% of the time overall, Karl gets lucky 22% of the time overall, and Tom gets lucky 15% of the time overall. That's about 70% of the time one of them gets lucky. The other 30% of the time, it drops through all the way with no one hitting. When you pick the lucky number in advance (not in succession only if the previous guy doesn't hit), then they each hit 33% of the time and there is always a winner.
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Two numbers are different but treated as equal. What the heck?

08 Oct 2020, 18:13

@boiler Yes, now both errors are obvious to me, thanks.

But it's not clear why - if I change the return statement to return Customer - the function sometimes returns the empty string. This is probably related to your last post, though I'm not sure.
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Two numbers are different but treated as equal. What the heck?

08 Oct 2020, 18:30

Yes, it’s because 30% of the time, no one matched the lucky number.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: RandomBoy, scriptor2016 and 360 guests