Created
October 3, 2023 08:41
-
-
Save trycf/c28d3828254bcccd52c4d90fc1b96b18 to your computer and use it in GitHub Desktop.
TryCF Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
try{ | |
debug = false; | |
questions = [ | |
{ questionid: 1, count: 1 }, | |
{ questionid: 2, count: 0 }, | |
{ questionid: 3, count: 1 }, | |
{ questionid: 4, count: 0 }, | |
{ questionid: 5, count: 0 }, | |
{ questionid: 6, count: 0 }, | |
{ questionid: 7, count: 0 }, | |
{ questionid: 8, count: 0 }, | |
{ questionid: 9, count: 0 }, | |
{ questionid: 10, count: 0 } | |
]; | |
// Choose questions, at random, but make sure that there is an equal distribution | |
public struct function chooseQuestionEqualDistribution(array items = [], boolean randomise = true) output="false" { | |
// sort items by count | |
items.sort( | |
function ( a, b ) { | |
if( b.count < a.count ) { | |
return 1; | |
} | |
if( b.count > a.count ) { | |
return -1; | |
} | |
return 0; | |
} | |
); | |
// pick first item with smallest count | |
var firstItem = items[1]; | |
if(debug) { | |
// WriteDump(var={firstItem: firstItem}); | |
// abort; | |
} | |
if(randomise) { | |
var minCounts = items.filter( function(item) { | |
return item.count === firstItem.count; | |
}); | |
if(debug) { | |
WriteDump(var={minCounts: minCounts}); | |
// abort; | |
} | |
var randIdx = Val(Floor(Rand() * ArrayLen(minCounts))); | |
var rndItmIdx = randIdx >= 1 ? randIdx : 1; | |
if(debug) { | |
WriteDump(var={rndItmIdx: rndItmIdx}); | |
// abort; | |
} | |
firstItem = minCounts[rndItmIdx]; | |
if(debug) { | |
WriteDump(var={firstItem: firstItem}); | |
// abort; | |
} | |
} | |
// increment that item's count | |
firstItem.count++; | |
if(debug) { | |
WriteDump(var={firstItem: firstItem}); | |
// abort; | |
} | |
return firstItem; | |
} | |
temp = { | |
'count_1': 0, | |
'count_2': 0, | |
'count_3': 0, | |
'count_4': 0, | |
'count_5': 0, | |
'count_6': 0, | |
'count_7': 0, | |
'count_8': 0, | |
'count_9': 0, | |
'count_10': 0 | |
}; | |
for (i = 1; i <= 15; i++){ | |
item = chooseQuestionEqualDistribution(questions); | |
// WriteDump(var={questions: questions}); | |
// abort; | |
temp['count_' & item.questionid] += 1; | |
if(debug) { | |
WriteDump(var={ | |
questions: questions, | |
temp: temp | |
}); | |
abort; | |
} | |
} | |
WriteDump(var={ | |
questions: questions, | |
temp: temp | |
}, label="SUCCESS"); | |
} | |
catch(any e){ | |
WriteDump(var={ | |
questions: questions, | |
temp: temp, | |
error: e | |
}, label="ERROR"); | |
} | |
</cfscript> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment