Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coin Toss - Distribution
#5
You are having a hard time understanding the code because the code is stupid. This does the same thing in a straight forward way.
import random

final_tails = []

for x in range(100) :
    tails = 0
    for x in range(10) :
        tails += random.randint(0, 1)
    final_tails.append(tails)  
print(final_tails)
Instead of counting the number of tails using an integer, the program saves the tails count as a history list. To show this I expanded the inner loop:
import random

results = ['Heads', 'Tails']
final_tails = []

tails = [0]
for x in range(10) :
    coin = random.randint(0, 1)
    tails.append(tails[x] + coin)
    print(results[coin], tails)
Output:
Heads [0, 0] Heads [0, 0, 0] Heads [0, 0, 0, 0] Tails [0, 0, 0, 0, 1] Tails [0, 0, 0, 0, 1, 2] Heads [0, 0, 0, 0, 1, 2, 2] Tails [0, 0, 0, 0, 1, 2, 2, 3] Heads [0, 0, 0, 0, 1, 2, 2, 3, 3] Heads [0, 0, 0, 0, 1, 2, 2, 3, 3, 3] Heads [0, 0, 0, 0, 1, 2, 2, 3, 3, 3, 3]
The list "tails" is how many tails have occurred so far. tails[-1] is the last value after ten coin flips. Looking back at the original code, "final_tails" is a collection of how many times the coin comes up tails when flipped 10 times.

I was surprised to learn that random.randint and np.random.randint are different.
Reply


Messages In This Thread
Coin Toss - Distribution - by lasek723 - Oct-04-2020, 12:26 PM
RE: Coin Toss - Distribution - by hshivaraj - Oct-04-2020, 12:33 PM
RE: Coin Toss - Distribution - by lasek723 - Oct-04-2020, 12:40 PM
RE: Coin Toss - Distribution - by hshivaraj - Oct-04-2020, 01:02 PM
RE: Coin Toss - Distribution - by deanhystad - Oct-04-2020, 01:07 PM
RE: Coin Toss - Distribution - by lasek723 - Oct-04-2020, 01:18 PM
RE: Coin Toss - Distribution - by deanhystad - Oct-04-2020, 01:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  distribution fit Glaucio 1 348 Apr-07-2024, 12:30 AM
Last Post: Larz60+
  Weight Distribution 11drk9 11 922 Mar-13-2024, 06:08 AM
Last Post: Pedroski55
  Are there errors in the code for my coin toss systems? Matlibplot is too perfect . Coolkat 0 454 Nov-13-2023, 11:54 AM
Last Post: Coolkat
Information Best distribution method inovermyhead100 0 629 Jul-19-2023, 07:39 AM
Last Post: inovermyhead100
  How do I use a whl puython distribution? barryjo 6 1,934 Aug-15-2022, 03:00 AM
Last Post: barryjo
  [split] Coin Flip Program Crackity 5 4,948 Sep-25-2017, 03:48 AM
Last Post: Crackity
  Coin Flip Program Warbit 6 8,306 Mar-27-2017, 11:33 PM
Last Post: Warbit

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020