A BASIC coin flip…

Us humans (this is not an A.I. post, bleep bloop) have a tendency to try to find patterns in randomness. For example, when asked to pick a number between 1 and 10, a magician/mentalist will know that statistically humans are more likely to choose certain numbers. There is alot of “human nature” that makes us somewhat predictable.

In a deck of 52 playing cards, if you were asked to predict what card is on the top of a shuffled deck, you probably wouldn’t say Ace of Diamonds, but that card is just as likely to be there as any other. No matter which card you guess, you have a 1 in 52 chance of being correct.

Call it in the air…

When it comes to a coin toss, do you always call heads? Always tails? Or do you alternate?

When a gambling casino game presents a grid of squares and asks you to pick four squares, do you “randomly” pick various squares, or do you just click the first four on the top row?

If it is random, either should have the same outcome.

And don’t get me started on picking lottery numbers. While we do not often see the picked numbers be “1, 2, 3, 4, 5 and 6”, that sequence should be just as likely as any other.

If it is random.

So let’s play a game in BASIC with a coin toss. Heads or tails will be represented using CoCo’s Color Basic RND() command. Doing RND(2) will produce either a 1 or 2 result.

NOTE: This is not random. This is psuedo random. I have discussed this previously, but for the sake of this blog post we will pretend it truly is random.

Would calling heads every time produce a better result than calling tails? Or would randomly choosing heads or tails each flip be better?

Let’s try…

0 'COINFLIP.BAS
5 'POKE 65495,0
10 W1=0:W2=0
20 FOR A=1 TO 1000
30 V=RND(2)
40 IF V=1 THEN W1=W1+1
50 IF V=RND(2) THEN W2=W2+1
60 NEXT
70 PRINT "ALWAYS GUESSING 1:";W1
80 PRINT "GUESSING RAND 1-2:";W2

This program will “randomly” flip a coin 1000 times and count how many times it landed on heads (1) versus how many times it matched a randomly (1-2) chosen value. At the end, it will print the results:

As you can see, in this “random” test, neither method really proved to be that different. We could also alter the output to print how many times guessing tails (2) would have worked (1000 clips minus how many times it was heads, 511 in this example, so 489 if my math is correct).

But it still feels better thinking we have some “control” over things and guessing rather than always choosing the same guess.

Alphabetically speaking…

Let’s modify the program to select a random letter, A-Z (represented by 1-26). We will now always guess A, versus randomly guess a letter (1-26):

0 'COINFLP2.BAS
5 'POKE 65495,0
10 W1=0:W2=0
20 FORA=1TO1000
30 V=RND(26)
40 IF V=1 THEN W1=W1+1
50 IF V=RND(26) THEN W2=W2+1
60 NEXT
70 PRINT"ALWAYS GUESSING 1:";W1
80 PRINT"GUESSING RND 1-26:";W2

And here is what I get…

Maybe this perspective will help you “always choose tails” or “always guess Aces of Spades” in the future.

And speaking of the future, there is another “random” test I want to experiment with, coming soon.

Until then…

10 thoughts on “A BASIC coin flip…

  1. MiaM

    This obviously mostly tests the quality of the random number generator.

    Re lottery numbers: You could analyze how often each number is selected for a particular lottery and go with the ones that have shown up the most often. If there were any imperfections in the then mechanical machines (like picking a random ball when turning a crank or whatnot) they would show up as certain numbers having a slightly higher chance of being selected. I would think that for any lottery that is larger than say “the touring regional carnival” would do their own analysis and adjust any imperfections. But for the “touring regional carnival” especially someone working at the carnival, but not directly involved in any lottery, could collect statistics and have a slightly higher chance at winning. Maybe.

    Reply
    1. Allen Huffman Post author

      Are you volunteering to flip a real coin 1000 times … for science? ;)

      This is all inspired by something “stupid” someone actually did on YouTube to test a random thing. Hoping to write more parts leading up to a similar simulation to see if it matches reality.

      Reply
      1. James Jones

        It’s not like we have much reason to believe human coin flippers are random, for that matter.

        Reply
    2. Allen Huffman Post author

      Hey, you know that “wall of lava lamps” that is used for random number generation? I wonder if they have an API or web page you can use to pull a truly random number. That would be an interesting way to see if it is much different.

      Reply
  2. JCCyC

    The trick I normally use when I’m in one of these very basic BASICs is take advantage of the human factor:

    10 PRINT “PRESS ANY KEY TO START THE DRAW”
    20 IF INKEY$<>”” THEN GOTO 40
    30 X=RND(0):GOTO 20
    40 PRINT “SLIGHTLY LESS PREDICTABLE RESULT IS”;X

    Reply
    1. Allen Huffman Post author

      To keep seeding, based on how long the user takes to hit a key. That is a good approach.

      With Extended Color BASIC, they added a TIMER and you could see with RND(-TIMER), but when I went to make my Lights Out program (for CoCo and VIC-20), elsewhere in this blog, I realized I could run on a 4K CoCo … but no TIMER. So I think I did something similar to generate a random seed.

      Reply
    2. MiaM

      Is this a difference between 6809 and 6502 Microsoft Basic?
      IIRC in 6502 basic you should use RND(1). Can’t remember the details about what happens if you use RND(0).
      (RND with negative numbers restarts the seed, and IIRC the randomness gets worst the larger the negative number gets. Just using the timer as a value to start the seed might thus not be a great idea).

      Btw some of the Sid Meyer games for the C64 used the ability to read the output signal from one of the oscillators in the SID chip, set to noise, as a random generator. Thus the game isn’t playable correctly if the SID chip is broken or if it uses one of the modern replacements (or you are using an emulator) that doesn’t emulate this feature.

      Btw, here are some random numers:
      -1
      17
      42
      4711
      11147
      :)

      Reply
      1. Allen Huffman Post author

        The Color BASIC uses a negative number as a seed. I feel like I looked up the VIC and it had something similar?

        On VIC you always did RND(1) or something and multiplied it by the upper limit? Is that correct?

        CoCo, is RND(1) or RND(100) or whatever to get that value, but it did support RND(0) for a 0-.9999 type range, which I assume is similar to CBM BASIC?

        That sampling or random noise is interesting. I wonder how the SID was creating it to begin with, since it would probably need its own random generator?

        Check out random.com!

        Reply

Leave a Reply to Allen HuffmanCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.