Hey mr_griff,
I took that hunk of code you posted and created what you see below.
Code: Select all
<?php
# The noise below is, I was told, a wieghted round robin algorithm. Looks rather ugly,
# but perhaps there is truth in what was said.
/*
<!--- The Weightings --->
<cfset ListItems = "One,Two,Three,Four,Five,Six">
<cfset ListWeightings = "1,3,5,11,30,50">
<!--- Get the number of Items in the List --->
<cfset OptionsNumber = ListLen(ListWeightings)>
<!--- Add up the List --->
<cfset OptionsTotal = Evaluate(Replace(ListWeightings, ",", "+", "ALL"))>
<!--- Set random number based on OptionsTotal --->
<cfset RandNum = RandRange(1, OptionsTotal)>
<!--- Default the ElectedOption to Zero --->
<cfset ElectedOption = 0>
<!--- Loop over list to get Option --->
<cfloop list="#ListWeightings#" index="Option">
<cfset ElectedOption = ElectedOption + 1>
<cfif Option GTE RandNum>
cfbreak>
<cfelse>
<cfset RandNum = RandNum - Option>
</cfif>
</cfloop>
*/
# The weightings
$ListItems = "One,Two,Three,Four,Five,Six";
$ListWeightings = "1,3,5,11,30,50";
# Get number of Items in list
$OptionsNumber = sizeof(explode(",", $ListWeightings));
# Add up the list
$OptionsTotal = array_sum(explode(",", $ListWeightings));
# Set random number based on OptionsTotal
mt_srand(make_seed());
$RandNum=mt_rand(1, $OptionsTotal);
# Set the default var for ElectedOption to 0
$ElectedOption = 0;
# Let's just create a $ListWeightings array. We could iterate over a string as
# CF does, but it's not worth the hassle
$arrListWeightings=explode(",", $ListWeightings);
# Now loop over the list to the the Option
while(list(, $val)=each($arrListWeightings))
{
++$ElectedOption;
if($val>=$RandNum)
{ break; }
else
{ $RandNum=($RandNum-$val); }
}
echo "$RandNum\n";
exit;
##################################################################
# Functions
##################################################################
# seed with microseconds
function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
?>
Now I'm stil a little lost on how it works to provide a weighted round robin kind of, err...,
anything. Also, the PHP version could easily be made more effecient by dropping the strings
and using arrays.
Some things are definitely interesting though. How CF provides a built in way to iterate over llists that are really strings is interesting, but it has got to slow the language down a bit. Also how the evaluate() function works was strange too.
It's a neat language in a sense, if not a little ugly. I like the comparison operators.
Code: Select all
PHP CF
------ ------
>= GTE
== IS
<= LTE
> GT
< LT
There are more I'm sure, but it's kinda cool, and perhaps a little more intuitive when looked at as acronyms.
Anyways, have more of the code so I can get more of an idea on how this is to be "plugged in"? There are a number of vars that are worked with at the beginning of the code that don't seem to contribute in any way to the final result.
Cheers,
BDKR