Search code examples
coldfusioncoldfusion-9

Working out decimal odds


Going over some old code

<!--- RANDOMLY DECIDE IS THIS PERSONS A WINNER 1 IN 8 CHANCE--->
<cfset attributes.random_point = RandRange(1, 8)>

<cfif attributes.random_point eq 5>
  WINNER
<cfelse>
  You got nothing!
</cfif>

How do I convert this to decimal odds? From now on I want to use the decimal odds to determine the change of getting x. So for example I have a 1.10 chance or 1.11 chance of getting x.


Solution

  • If I understand correctly then I would define the % chance of winning as a decimal and then rely on Rand(), though this still doesn't feel ideal

    <cfscript>
        numChanceToWin = 0.15;  // <= this == winner == 15% chance to win
        numRandom = Rand(); // decimal from 0 to 1
    
        if (numRandom <= numChanceToWin ) {
            // Winner
        } else {
            // Loser
        }
    <cfscript>
    

    Also works with your 1/8 definition by setting numChanceToWin = 1/8; or even if (numRandom/numChanceToWin <= 1) { // Winner