Python Forum
Random module, coin flipping
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random module, coin flipping
#4
This is homework and following may not apply.

To simulate coin flipping one can just:

>>> random.choices(['heads', 'tails'], k=5)
['tails', 'tails', 'heads', 'tails', 'tails']
To verify that it its evenly distributed:

>>> random.choices(['heads', 'tails'], k=1000).count('heads')        
501
One can add weights or cumulative weights ('biased coin'):

>>> random.choices(['heads', 'tails'], cum_weights=[0.70, 1.00], k=1000).count('heads')                                               
703
Result can be printed out using unpacking and sep:

>>> print(*random.choices(['heads', 'tails'], k=5), sep='\n') 
tails
heads
heads
tails
heads
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Random module, coin flipping - by gus17 - Jan-05-2020, 06:58 PM
RE: Random module, coin flipping - by ichabod801 - Jan-05-2020, 07:55 PM
RE: Random module, coin flipping - by sandeep_ganga - Jan-06-2020, 07:26 AM
RE: Random module, coin flipping - by perfringo - Jan-06-2020, 10:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Print the frequency of each coin for the combinations that sum to the amount N Pranav 3 2,605 May-19-2020, 06:16 AM
Last Post: Pranav
  Need help with coin change program Gateux 2 6,113 Jun-25-2019, 02:32 PM
Last Post: perfringo
  Adding and Removing coins to match Coin Bag Total infinite times Strayfe 8 4,703 Sep-11-2018, 07:30 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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