Avoiding FIVE Aces in a RND(0) Deal

By Welopez

In the Old West, if a player's hand held five Aces, most likely the player or the dealer would quickly have left the game by stopping a bullet. In the Modern West, such strong measures are frowned upon and the only thing keeping a careless programmer from that bullet is our respect for the law.

With an honest deck of cards, the probability the next card dealt will be an Ace is 4/52, or 1/13. If your program is dealing five RND(0) cards to five players, a total of 25 cards, it's very likely there may be five or more Aces on the table. Bummer!

The following code will check your array and force a re-deal before five Aces can be dealt.

      'Filling a Test Array

      DIM test(25)

      start=TIME$("milliseconds")

      FOR k=1 to 25 'Fill 25 array elements

      [tryAgain] 'Start here if a number has been repeated
      flag=0 'Reset the flag to 0 for next try

      GOSUB [chooseNum] 'Get a random number
          FOR i=1 TO 25 'Check the array to see if number currently exists
              IF num=test(i) THEN flag=1 : EXIT FOR 'If num exists, quit comparing
          NEXT i
          IF flag=1 THEN [tryAgain] 'If num has been used [tryAgain]
          test(k)=num 'If num has not been used, put it in the array

      NEXT k

      finish=TIME$("milliseconds") 'Success! We're done with this routine!

      FOR k=1 TO 25
          PRINT test(k), 'Show the values in the array
          ln=ln+1
          IF ln=5 THEN PRINT : ln=0 'For alignment of the output
      NEXT k

      PRINT 'Show results
      PRINT "The array was filled in ";finish-start;" milliseconds."
      PRINT "There were "; visits; " visits required to select a random number."

      END

      [chooseNum]
          num=INT(RND(0)*25)+1
          visits=visits+1
      RETURN

In this code, we are using a deck of only 25 cards, and we check to insure no card is dealt more than once. You can modify this code for numerical or string values, and shorten it significantly be removing remarks, etc. Because we are dealing 25 out of 25 cards, the probability of repeating a card increases with every card dealt, sort of like playing Russian Roulette with 5 loaded chambers! Very un-healthy!

The array has 25 elements, all of which are 0 when the program begins. We fill the array using a GOSUB a minimum of 25 times, assuming no random number is repeated. To see if a value exists in the current array, we use a second loop to compare the random value selected to every value currently in the array, before assigning it to the array.

If the value exists, we EXIT FOR and [tryAgain] to find a number which does not exist. If the value does not exist, we put it in the array and deal another card.

Filling a small array should not take long, in this routine, seldom more than 15 milliseconds, but may require many visits to the sub-routine to choose a random number for comparison. If your array has lots of elements, it may take much longer to find a random number which has not been used. Fortunately, modern PC's are faster than even the slickest of card sharps!

Who would have thought smart programming could prevent a coder's untimely end