Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coin Toss - Distribution
#1
I am currently doing the case studies from DataCamp - this script is confusing me

for x in range(100) :
    tails = [0]
    for x in range(10) :
        coin = np.random.randint(0, 2)
        tails.append(tails[x] + coin)
    final_tails.append[b](tails[-1])[/b]    
print(final_tails) 
Why do you type final_tails [-1] and not [1] (Bolded part of the script)

Thanks :-)
Reply
#2
Negative indexing reads value from backwards. For instance

>>> a = [1, 2, 3]
>>> a[-1]
3
>>> a[-2]
2
>>> a[0]
1
>>> a[1]
2
I hope that give some explanation of the behaviour
Reply
#3
(Oct-04-2020, 12:33 PM)hshivaraj Wrote: Negative indexing reads value from backwards. For instance

>>> a = [1, 2, 3]
>>> a[-1]
3
>>> a[-2]
2
>>> a[0]
1
>>> a[1]
2
I hope that give some explanation of the behaviour

I understand the negative indexing part using a list. I'm trying to understand how it works on the coin toss example

If you threw tails 4 times .... wouldn't -4 + 0 (from tails = [0} ........ come to -4?

Apologies for not understanding .... I'm a newbie :-)
Reply
#4
Frankly I dont see what the script is doing. Your using variable x twice which is going to cause issues.

Remember tail as a list is created every time for the outer loop and in the inner loop being modified by appending more elements to it and then finally the last element from the list if being considered to be printed.

Would good to share the source and the problem statement for this code to get more context.
Reply
#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
#6
(Oct-04-2020, 01:07 PM)deanhystad Wrote: 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.

You've explained this very well - Thanks. And your code seems much more simple to understand.

Where would you advise to learn python? DataCamp in some cases uses complicated ways of explaining some things .... thus resulting me to use YouTube.
Reply
#7
Sometimes stupid code is used to demonstrate an idea for learning purposes. If I wanted to write a program to evaluate coin toss distribution it is stupid to use lists like this. But if I want to teach how to uses lists and provide an example of using negative indexing this is not that bad of a program.

I think it is a good that you are confused and have to use multiple resources. Programming cannot be taught. You have to learn how to program by yourself. A programming class guides your learning, but you have to make the mistakes or do the extra research to really understand what the class is presenting. If you understood everything presented in the class that just means you already knew the material and there was no reason to take the class. Even concepts presented in a clear and concise manner require time and effort to internalize and become useful.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Weight Distribution 11drk9 11 516 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 342 Nov-13-2023, 11:54 AM
Last Post: Coolkat
Information Best distribution method inovermyhead100 0 530 Jul-19-2023, 07:39 AM
Last Post: inovermyhead100
  WARNING: Ignoring invalid distribution kucingkembar 1 24,309 Sep-02-2022, 06:49 AM
Last Post: snippsat
  How do I use a whl puython distribution? barryjo 6 1,694 Aug-15-2022, 03:00 AM
Last Post: barryjo
  Pyinstaller distribution file seems too large hammer 4 2,633 Mar-31-2022, 02:33 PM
Last Post: snippsat
  Need learning resources for Python distribution gretchenfrage 2 2,092 Nov-12-2020, 06:42 PM
Last Post: gretchenfrage
  [split] Coin Flip Program Crackity 5 4,739 Sep-25-2017, 03:48 AM
Last Post: Crackity
  Coin Flip Program Warbit 6 8,022 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