tracker issue : CF-4203530

select a category, or use search below
(searches all categories and all time range)
Title:

RandRange does not sum correctly in struct with shorthand addition

| View in Tracker

Status/Resolution/Reason: Closed/Withdrawn/AsDesigned

Reporter/Name(from Bugbase): Daniel Roberts / ()

Created: 11/26/2018

Components: Language, Expressions

Versions: 2016,2018

Failure Type: Incorrect w/Workaround

Found In Build/Fixed In Build: multiple / NA

Priority/Frequency: Major / Very few users will encounter

Locale/System: / Windows 7 64-bit

Vote Count: 0

Problem Description:

- When passing RandRange function as struct key value and using shorthand addition notation, the struct values do not sum correctly.

Steps to Reproduce:

<cfset checks = {1=0, 2=0, count=0} />
<cfloop from="1" to="1000" index="ix">
    <cfset checks["count"] += 1 />
    <cfset checks["#randrange(1,2)#"] += 1 />
</cfloop>
<cfdump var="#checks#" format="text" />

Actual Result:

struct
1: 526
2: 527
COUNT: 1000

- When looping over a struct with random addition for two values the total should be the same as the number of loop iterations. However the total is usually many more or less than the number of iterations. 

Expected Result:

- Adding accumulated values should total the count but they never do.

Any Workarounds:

- setting the RandRange result to a variable first and then passing as the struct key doesn't present the problem
- if incrementing by 1 then the increment ++ operator acts correctly

Attachments:

Comments:

Hi Daniel, <cfset checks = \{1=0, 2=1} /> <cfset checks[ #randrange(1,2)#] += 1 /> can be read as : <cfset checks [ #randrange(1,2)#] =checks [ #randrange(1,2)#] + 1 /> and then on runtime evaluation it can provide ways of initialization : <cfset checks[0] =checks[0] + 1 /> , or <cfset checks[1] =checks[1] + 1 /> , or <cfset checks[0] =checks[1] + 1 /> ,or <cfset checks[1] =checks[0] + 1 /> and , last two might give you different count.   I believe it is the expected behavior.   Thanks, Mukesh
Comment by Mukesh K.
31022 | July 15, 2019 07:16:39 AM GMT