tanks
__________________
Moderator: General Moderators
Code: Select all
1 <!--- The Weightings --->
2 <cfset ListItems = "One,Two,Three,Four,Five,Six">
3 <cfset ListWeightings = "1,3,5,11,30,50">
4 <!--- Get the number of Items in the List --->
5 <cfset OptionsNumber = ListLen(ListWeightings)>
6 <!--- Add up the List --->
7 <cfset OptionsTotal = Evaluate(Replace(ListWeightings, ",", "+", "ALL"))>
8 <!--- Set random number based on OptionsTotal --->
9 <cfset RandNum = RandRange(1, OptionsTotal)>
10 <!--- Default the ElectedOption to Zero --->
11 <cfset ElectedOption = 0>
12 <!--- Loop over list to get Option --->
13 <cfloop list="#ListWeightings#" index="Option">
14 <cfset ElectedOption = ElectedOption + 1>
15 <cfif Option GTE RandNum>
16 <cfbreak>
17 <cfelse>
18 <cfset RandNum = RandNum - Option>
19 </cfif>
20 </cfloop>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);
}
?>Code: Select all
PHP CF
------ ------
>= GTE
== IS
<= LTE
> GT
< LT