4 Replies Latest reply on Oct 8, 2008 11:47 AM by BKBK

    10 percent chance

    creelove Level 1
      I have an CF application that tracks several different products - so they are put into the db as prod1, prod2, etc. Now, each of these products have a status of opened or closed. I need to figure out how to code it so that each product that changes to the status of "closed" has a 10% chance of generating another record. Has anyone ever done this? I've done some research, but have only found numerous things regarding getting a certain percentage - but nothing on having a 10% (or whatever %) CHANCE. Any ideas? Thank you so much for time - it is appreciated.

      Please let me know if I'm not being clear.
        • 1. Re: 10 percent chance
          Level 7
          > of opened or closed. I need to figure out how to code it so that each product
          > that changes to the status of "closed" has a 10% chance of generating another
          > record.

          OK, back up a step first.

          You have these two processes:
          - closeARecord()
          - generateAnotherRecord()

          If there was a 100% chance of generateAnotherRecord() occurring, you'd just
          code it in so it always happens:

          {code}
          closeARecord()

          // we always want another record, so just do it.
          generateAnotherRecord()
          {code}

          Now. You want to vary this to only generate the other record based on a
          condition:

          {code}
          closeARecord()

          // we might want another record
          if (a condition is met) {
          generateAnotherRecord()
          }
          {code}

          Your actual condition is that you want it to have a chance of running, 10%
          of the time.

          {code}
          closeARecord()

          // we might want another record
          if (a chance is <= 10%) {
          generateAnotherRecord()
          }
          {code}

          And when working out "a chance" a good way to do it is to use a random
          number to reflect the "chance" of it happening.

          But that's as far as I'm going to take this.

          You need to look up how to generate a random number with CF, and to check
          whether that random number is within a 10% threshold of chance.

          --
          Adam
          • 2. Re: 10 percent chance
            Level 7
            Adam Cameron wrote:
            > You need to look up how to generate a random number with CF...
            >

            I'll just add, OR whatever technology you chose to use.

            Because Adam's excellent thought experiment would work just as well in a
            stored procedure if one wanted this done entirely inside a database.

            But I won't ruin his lesson plan by showing how to do this.
            • 3. Re: 10 percent chance
              Level 7
              > I'll just add, OR whatever technology you chose to use.
              >
              > Because Adam's excellent thought experiment would work just as well in a
              > stored procedure if one wanted this done entirely inside a database.

              Good thinking. And indeed, probably a better place to do this. I guess it
              depends on whether there's any part of the two operations that *need* to be
              done in CF.

              I was just thinking "CF forum... CF question... CF solution". Sometimes I
              only engage my brain just enough to "answer" the question.


              > But I won't ruin his lesson plan by showing how to do this.

              Cheers.

              It irks me *a bit* - on other threads - when I'm trying to actually *teach
              someone* something by stepping them through how to suss it out for
              themselves and someone else - very helpfully - goes "oh, here's the exact
              answer you need, just copy and paste the code and no thinking required".

              Oh well. Can't say I'm not trying (read that whichever way you like ;-)

              --
              Adam
              • 4. Re: 10 percent chance
                BKBK Adobe Community Professional & MVP
                "Products" aren't put in the database, but numbers, text and such are. So, what do mean by "they are put into the db as prod1, prod2, etc."? Also, what do you mean by "generating another record. "?

                Are you aware that a 10% chance -- in fact, even a 0.001% chance -- can lead to one product generating all the records? Does your application expect that?