Python Forum
NEED HELP Pseudo-Random Numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NEED HELP Pseudo-Random Numbers
#1
Hey everyone. I've been coding for about a month now. Trying to figure out the assignment I got this week. THe assignment asked me to find if there were any shutouts in a simulation of games and report them. I defined shutout() and Summary(). I feel I am headed in the right direction. I just need a few more pointers.

from random import random

def main():
    printIntro()
    probA, probB, n = getInputs()
    winsA, winsB = simNGames (n, probA, probB)
    printSummary(winsA, winsB)
    Summary(shutoutsA, shutoutsB)

def printIntro():
    print("THis program simulates a game of racquetball between two")
    print('players called "A" and "B". The ability of each player is')
    print("the player wins the point when serving. Player A alwyas")
    print("has the first serve.")

def getInputs():
    #Returns the three simulation parameters
    a = float(input("What is the prob. player A wins a serve? "))
    b = float(input("What is the prob. player B wins a serve? "))
    n = int(input("How many games to simulate? "))
    return a, b, n

def simNGames(n, probA, probB):
    #Simulates n games of racquetball between players whose
    #   abilities are represented by the probability of winning a serve.
    #Returns number of wins for A and B
    winsA = winsB = 0
    for i in range(n):
        scoreA, scoreB = simOneGame(probA, probB)
        if scoreA > scoreB:
            winsA = winsA + 1
        else:
            winsB = winsB + 1
    return winsA, winsB

def simOneGame(probA, probB):
    #Simulates a single game of racquetball between players whose
    # abilities are represented by theprobability of winning a serve.
    #Returns final scores for A and B
    serving = "A"
    scoreA = 0
    scoreB = 0
    shutoutsA = 0
    shutoutsB = 0
    while not gameOver(scoreA,scoreB):
        if serving == "A":
            if random() < probA:
                scoreA = scoreA + 1
            else:
                serving = "B"
                
        else:
            if random() < probB:
                scoreB = scoreB + 1
            else:
                serving = "A"
        
    return scoreA, scoreB

def shutout(scoreA, scoreB):
    shutoutsA = 0
    shutoutsB = 0
    while simNGames(probA, probB):
        if scoreA == 0:
            shutoutsB = shutoutsB + 1
        if scoreB == 0:
            shutoutsA = shutoutsA + 1
    return shutoutsA, shutoutsB
    

def gameOver(a, b):
    #a and b represent scores for a racquetball game
    #Returns True if the game is over, False otherwise.
    return a==15 or b==15

def printSummary (winsA, winsB,):
    #Prints a summary of wins for each player.
    n = winsA + winsB
    print("\nGames simulated:", n)
    print("Wins for A: {0} ({1:0.1%})".format(winsA, winsA/n))
    print("Wins for B: {0} ({1:0.1%})".format(winsB, winsB/n))
    
def Summary (shutoutsA, shutoutsB):
    print(shutoutsA)
    print(shutoutsB)


if __name__== '__main__': main()
Reply


Messages In This Thread
NEED HELP Pseudo-Random Numbers - by Kongurinn - Oct-20-2017, 01:01 AM
RE: NEED HELP Pseudo-Random Numbers - by ichabod801 - Oct-20-2017, 01:04 AM
RE: NEED HELP Pseudo-Random Numbers - by Kongurinn - Oct-20-2017, 01:10 AM
RE: NEED HELP Pseudo-Random Numbers - by ichabod801 - Oct-20-2017, 01:20 AM
RE: NEED HELP Pseudo-Random Numbers - by Kongurinn - Oct-20-2017, 01:25 AM
RE: NEED HELP Pseudo-Random Numbers - by ichabod801 - Oct-20-2017, 02:10 AM
RE: NEED HELP Pseudo-Random Numbers - by Skaperen - Oct-20-2017, 04:00 AM
RE: NEED HELP Pseudo-Random Numbers - by Kongurinn - Oct-22-2017, 05:11 AM
RE: NEED HELP Pseudo-Random Numbers - by Larz60+ - Oct-22-2017, 08:37 AM
RE: NEED HELP Pseudo-Random Numbers - by Skaperen - Oct-23-2017, 04:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,477 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 3,081 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Stein's GCD according to given pseudo code wanttolearn 2 2,024 Aug-27-2020, 07:58 PM
Last Post: wanttolearn
  Scaling random numbers within specific range schniefen 4 3,232 Oct-28-2019, 11:22 AM
Last Post: jefsummers
  How to Generate and Print An Array with Random Numbers in Python in the johnnynitro99293 14 9,754 Sep-20-2019, 11:56 AM
Last Post: ichabod801
  Write pseudo code for a class assignment Scrimshot 3 3,447 May-07-2019, 05:38 PM
Last Post: Scrimshot
  random numbers and stats cliffhop23 2 6,953 Feb-22-2018, 09:16 PM
Last Post: glidecode
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,150 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE
  List with Random Numbers AnjyilLee 5 9,450 Oct-14-2017, 09:22 PM
Last Post: buran
  pseudo code for a quiz tsaward 4 7,761 Sep-15-2017, 09:59 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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