counting runs

N Cueto nicolas_cueto at yahoo.com
Mon Oct 31 20:26:46 EST 2005


Hi,

Below is my suggestion. It allows the
number of flips and coin sides to be
varied, and can tally events of un-
limited length.

Cheers,
Nicolas Cueto


on mouseUp
  
  -- PREP THE VARIABLES
  put 100 into tNumberOfFlips
  put "HT" into tCoin
  put the number of characters in tCoin into tRandSeed
  put empty into tThePreviousToss
  put empty into tTheRun
  put empty into tTheSeries
  
  -- START FLIPPING THE COIN
  repeat with i = 1 to tNumberOfFlips
    put char (random(tRandSeed)) of tCoin into tTheCurrentToss
    if tTheCurrentToss <> tThePreviousToss then 
      -- THE RUN IS BROKEN
      put tTheRun & cr after tTheSeries
      put tTheCurrentToss into tTheRun
    else 
      -- THE RUN CONTINUES
      put tTheCurrentToss after tTheRun
    end if
    put tTheCurrentToss into tThePreviousToss
  end repeat
  delete the last char of tTheSeries
  sort lines of tTheSeries
  delete line 1 of tTheSeries
  
  -- COUNT THE NUMBER OF EVENTS FOR EACH PARTICULAR RUN
  put empty into tThePreviousLine
  put 1 into tEventCount
  put empty into tFinalEventCounts
  repeat for each line tTheCurrentLine in tTheSeries
    if tTheCurrentLine <> tThePreviousLine then
      -- A NEW EVENT
      put "Event count for run-type " & tThePreviousLine & \
          ": " & tEventCount & cr after tFinalEventCounts
      put 1 into tEventCount
    else
      -- THE PREVIOUS EVENT CONTINUES
      add 1 to tEventCount
    end if
    put tTheCurrentLine into tThePreviousLine
  end repeat
  delete line 1 of tFinalEventCounts
  put tFinalEventCounts into field "fResult"

end mouseUp



More information about the Use-livecode mailing list