I was wondering if I can get some help looking at the second for loop. In this case, the die is not fair, and I have the die weighted heavily for even numbers. There are two issues with the code: the probabilities do not add up to 1, and the probability of the odd numbers is larger than the even numbers.
import random
NUM_ROLLS = 100
DIE_SIDES = [1,3,2,4,4,6]
odd_roll_result = 0
even_roll_result = 0
probability = 0
# Create the dictionary to store the results of each roll of the die.
rolls = {}
#Loop for rolling the die NUM_ROLLS times
for r in range(NUM_ROLLS):
roll_result = random.randint(0,len(DIE_SIDES)-1)
if roll_result in rolls:
# Add to the count for this number.
rolls[roll_result] += 1
else:
# Record the first roll result for this number.
rolls[roll_result] = 1
#print(DIE_SIDES[roll_result])
# Print how many times each number was rolled
for roll_result in range(len(DIE_SIDES)):
# print("The number", str(roll_result).format(roll_result, DIE_SIDES[roll_result]),
# "was rolled", str(rolls[roll_result]), "times.")
print("The number", roll_result, DIE_SIDES[roll_result],
"was rolled", str(rolls[roll_result]), "times.")
if roll_result == 1 or roll_result == 3 or roll_result == 5:
odd_roll_result = odd_roll_result + rolls[roll_result]
#print(odd_roll_result)
if roll_result == 2 or roll_result == 4 or roll_result == 6:
even_roll_result = even_roll_result + rolls[roll_result]
#print(even_roll_result)
probability_odd = odd_roll_result/NUM_ROLLS
th_exp = 1/6
prob_th = abs(th_exp-probability_odd)
probability_even = even_roll_result/NUM_ROLLS
print("The probability of getting an odd number is " , probability_odd )
print("Theoretical probability of getting an odd number is " , prob_th )
print("The probability of getting an even number is " , probability_even )
First, as other commenters have pointed out, you should fix the definition of DIE_SIDES:
DIE_SIDES = [1,2,3,4,5,6]
Then change the line that rolls the dice like this:
roll_result = random.randint(1,len(DIE_SIDES))
Or even better:
roll_result = random.choice(DIE_SIDES)
Your code considers 0 a roll_result, but does not count it in either odd_roll_result or even_roll_result. This is why the probabilities do not add up to 1.
Related
i am pretty much starting to learn code so my knowledge is limited.
following scenario:
there is this "risk" game where playerA rolls a dice three times and playerB two times. now the two highest results of playerA are compared to the two of player B. if the highest result of A is greater than playerBs, player A gets a point, otherwise (<=) B gets a point. The same for the second highest result of A and B. So the total results of the points could be: 2:0, 1:1 or 0:2
Now the question:
The are obviously 6^5=7776 ways to dice and 2890 of them lead to 2:0, 2611 to 1:1 and 2275 to 0:2.
How can i show this statistic by printing out for example "There are 2890 possibilities to win 2:0" ?
I am able to show for a random dice roll who wins the game, but not for all.
I would be very thankful for some help.
import random
r1 = random.randint(1,6)
r2 = random.randint(1,6)
r3 = random.randint(1,6)
b1 = random.randint(1,6)
b2 = random.randint(1,6)
Points_A = 0
Points_B = 0
Dice_A = [r1,r2,r3]
Dice_B = [b1,b2]
print("results of A: ", Dice_A)
print("results of B: ", Dice_B)
A = sorted(Würfel_A)
B = sorted(Würfel_B)
if A[2] > B[1]:
Points_A += 1
elif A[2] == B[1]:
Points_B +=1
else: Points_B += 1
if A[1] > B[0]:
Points_A += 1
elif A[1] == B[0]:
Points_B += 1
else: Points_B += 1
print("A has ", Points_A, "Points.")
print("B has ", Points_B, "Pionts.")
Here is how I would do it:
Create a function that casts a given number of dice and sorts the results in descending order, and returns that list. For example cast_dice(3) could return [6, 4, 1]
Use that function to produce a result for the red player and the blue player. Then zip those two lists (the third die of the red player will not play a role) so the mutual dice can be compared. Calculate the number of dice with which red wins. Red can win with either 0, 1 or 2 dice.
To get statistics on distribution, you could create a list with three counters: one for each possible red-score. Run the above procedure thousands of times and to populate this list of three.
Calculate an average score from that list of three
Here is an implementation:
from random import randint
def cast_dice(dice_count):
return sorted((randint(1, 6) for _ in range(dice_count)), reverse=True)
def get_stats(num_rounds):
score_counts = [0, 0, 0] # red can score 0, 1 or 2 per round
for round in range(num_rounds):
reds = cast_dice(3)
blues = cast_dice(2)
score = sum(int(red > blue) for red, blue in zip(reds, blues))
score_counts[score] += 1
return score_counts
score_counts = get_stats(10000)
red_wins = score_counts[1] + 2 * score_counts[2]
blue_wins = score_counts[1] + 2 * score_counts[0]
avg_score = red_wins / sum(score_counts)
print("Raw score counts: ", score_counts)
print("Number of winning red dice: ", red_wins)
print("Number of winning blue dice: ", blue_wins)
print("Average score for red per round: ", avg_score) # ~1.08...
print("Average score for blue per round: ", 2 - avg_score) # ~0.92...
print("For every blue winning die, red has", red_wins / blue_wins, "winning dice")
So this sampling suggests that the expected outcome per round is that red wins one, and blue wins one, but that red has a tiny advantage: it will happen a bit more often that red wins two than that blue wins two.
This corresponds to the mathematical results you provided in the question.
To put it a bit differently, after 13 rounds (of 3 dice against 2), blue will have lost 2 "armies" more than red (on average).
The intention of this code is to run in a loop until a specified amount of turns or until I roll snake eyes(both die are = 1). I am not entirely sure what specifically is not working in my code so far.
import random
def play_game(rolls, win_turns):
win_turns = 0
rolls = []
i = 2
for _ in range(2):
roll = random.randint(1, i)
rolls.append(roll)
win_turns= win_turns + 1
i = i + 2
if roll in rolls == 2:
return win_turns
You have a number of problems here. Your "loop" only picks the two dice -- you don't loop until a loss. The "roll in rolls == 2" doesn't mean anything. This basically does what you ask:
import random
def play_game(turns):
win_turns = 0
for _ in range(turns):
die = random.randint(1,6), random.randint(1,6)
print("Roll:", die )
if sum(die) == 2:
break
win_turns += 1
return win_turns
print("Wins:", play_game(10))
I've created a function winProbability(ra, rb, n) and I want to simulate n games in order to estimate the probability that a player with the ability ra will win a game against a player with ability rb
I'll show the code I've done so far. If this seems like a easy issue it's because I am new to coding.
import random #import random allows for the use of randomly generated numbers
def game(ra, rb): #this function game sets out the way the game runs
p_a_point = ra/(ra+rb) #this line of code determines the probability that
#player a wins any given point
a_points = 0 #the amount of points player a has is defaulted to 0
b_points = 0 #the amount of points player b has is defaulted to 0
score_to_win = 11 #the winning score is defaulted to 11
while (a_points < score_to_win and b_points < score_to_win) or abs (a_points - b_points) < 2: #while player a's points and player b's points are less than the winning score:
p_b_point = random.random()#the probability b wins a point is set the a random value between 0 and 1
if p_b_point < p_a_point: #is the probability b wins a point is less than the probability a wins a point:
a_points = a_points + 1 #a wins 1 point
else: #if player a doesn't win a point:
b_points = b_points + 1 #b wins a point
return(a_points, b_points)#both players points are returned
print(game(70,30))#the function is called with two integer values as parameters
def winProbability(ra, rb, n):
To be honest from here I am unsure on how to go about this. I was thinking of doing a for loop so for example:
for n in game (ra, rb):
but I am unsure if I can use a previous function in this loop call. I'm also confused on how to calculate probabilities in code
The general aim is to call the function with two probabilities for example 70 and 30 and give a decimal answer for the probability player ra will win.
To previous commenters, I apologise for being vague before. I've never posted on here before.
See if this helps.
from random import randint, seed
seed()
rounds = input(" How many rounds will be played in the match? ")
print("\n Please enter skill levels as integers from 0 to 99.\n")
a = input(" What is the skill level of player 1? ")
b = input(" What is the skill level of player 2? ")
# Catch empty inputs
if not rounds: rounds = 10
if not a: a = 0
if not b: b = 0
# Python inputs are always strings. Convert them to integers.
rounds = int(rounds)
a = int(a)
b = int(b)
# If both skill levels are 0, set them to 1.
# (This will avoid a possible division by zero error.)
if a+b == 0: a = b = 1
# Catch and correct numbers that are too high.
if a > 99: a = 99
if b > 99: b = 99
# Convert integer skill levels to values between 0.0 and 0.99
a = a/100
b = b/100
print()
print(" Chance player 1 will win: "+str(int(100*a/(a+b)))+" percent.")
print(" Chance Player 2 will Win: "+str(int(100*b/(a+b)))+" percent.")
print()
for x in range(rounds):
roll = randint(0,999)/1000
print("roll =",roll, end =": ")
if roll <= a/(a+b): # <-- Compare roll to ratio of player skill levels.
print("Round "+str(x+1)+" Winner: Player 1")
else:
print("Round "+str(x+1)+" Winner: Player 2")
print()
this was my answer
import random
def winProbability(ra, rb, n):
winCount = 0 #used to count how many times 'a' won
probabilityRange = ra + rb
for i in range(n):
# pick a number between 0 and probabiilityRange
number = random.randint(0, probabilityRange)
# if number is < ra then 'a' won if number is > ra then 'b' won if number == ra then results in a draw
if number < ra:
winCount += 1
print ('win')
if number > ra:
print('loss')
if number == ra:
print ('draw') # draw doesn't count as win
return winCount*(100/n)
print (winProbability(10000,1,100000))
This prints the results of each game played, and returns the possibility that 'a' will win in percentile form.
I am trying to find the occurrences of each number for sides going 1 up to the number of sides on a dice roll. I would like the program to find the number of occurrences for each number that is in listRolls.
Example: if there were a 6 sided dice then it would be 1 up to 6 and the list would roll the dice x amount of times and I would like to find how many times the dice rolled a 1 so on and so forth.
I am new to python and trying to learn it! Any help would be appreciated!
import random
listRolls = []
# Randomly choose the number of sides of dice between 6 and 12
# Print out 'Will be using: x sides' variable = numSides
def main() :
global numSides
global numRolls
numSides = sides()
numRolls = rolls()
rollDice()
counterInputs()
listPrint()
def rolls() :
# for rolls in range(1):
###################################
## CHANGE 20, 50 to 200, 500 ##
##
x = (random.randint(20, 50))
print('Ran for: %s rounds' %(x))
print ('\n')
return x
def sides():
# for sides in range(1):
y = (random.randint(6, 12))
print ('\n')
print('Will be using: %s sides' %(y))
return y
def counterInputs() :
counters = [0] * (numSides + 1) # counters[0] is not used.
value = listRolls
# if value >= 1 and value <= numSides :
# counters[value] = counters[value] + 1
for i in range(1, len(counters)) :
print("%2d: %4d" % (i, value[i]))
print ('\n')
# Face value of die based on each roll (numRolls = number of times die is
thrown).
# numSides = number of faces)
def rollDice():
i = 0
while (i < numRolls):
x = (random.randint(1, numSides))
listRolls.append(x)
# print (x)
i = i + 1
# print ('Done')
def listPrint():
for i, item in enumerate(listRolls):
if (i+1)%13 == 0:
print(item)
else:
print(item,end=', ')
print ('\n')
main()
Fastest way (I know of) is using Counter() from collections (see bottom for dict-only replacement):
import random
from collections import Counter
# create our 6-sided dice
sides = range(1,7)
num_throws = 1000
# generates num_throws random values and counts them
counter = Counter(random.choices(sides, k = num_throws))
print (counter) # Counter({1: 181, 3: 179, 4: 167, 5: 159, 6: 159, 2: 155})
collections.Counter([iterable-or-mapping])) is a specialized dictionary that counts the occurences in the iterable you give it.
random.choices(population, weights=None, *, cum_weights=None, k=1) uses the given iterable (a range(1,7) == 1,2,3,4,5,6 and draws k things from it, returning them as list.
range(from,to[,steps]) generates a immutable sequence and makes random.choices perform even better then when using a list.
As more complete program including inputting facecount and throw-numbers with validation:
def inputNumber(text,minValue):
"""Ask for numeric input using 'text' - returns integer of minValue or more. """
rv = None
while not rv:
rv = input(text)
try:
rv = int(rv)
if rv < minValue:
raise ValueError
except:
rv = None
print("Try gain, number must be {} or more\n".format(minValue))
return rv
from collections import Counter
import random
sides = range(1,inputNumber("How many sides on the dice? [4+] ",4)+1)
num_throws = inputNumber("How many throws? [1+] ",1)
counter = Counter(random.choices(sides, k = num_throws))
print("")
for k in sorted(counter):
print ("Number {} occured {} times".format(k,counter[k]))
Output:
How many sides on the dice? [4+] 1
Try gain, number must be 4 or more
How many sides on the dice? [4+] a
Try gain, number must be 4 or more
How many sides on the dice? [4+] 5
How many throws? [1+] -2
Try gain, number must be 1 or more
How many throws? [1+] 100
Number 1 occured 22 times
Number 2 occured 20 times
Number 3 occured 22 times
Number 4 occured 23 times
Number 5 occured 13 times
You are using python 2.x way of formatting string output, read about format(..) and its format examples.
Take a look at the very good answers for validating input from user: Asking the user for input until they give a valid response
Replacement for Counter if you aren't allowed to use it:
# create a dict
d = {}
# iterate over all values you threw
for num in [1,2,2,3,2,2,2,2,2,1,2,1,5,99]:
# set a defaultvalue of 0 if key not exists
d.setdefault(num,0)
# increment nums value by 1
d[num]+=1
print(d) # {1: 3, 2: 8, 3: 1, 5: 1, 99: 1}
You could trim this down a bit using a dictionary. For stuff like dice I think a good option is to use random.choice and just draw from a list that you populate with the sides of the dice. So to start, we can gather rolls and sides from the user using input(). Next we can use the sides to generate our list that we pull from, you could use randint method in place of this, but for using choice we can make a list in range(1, sides+1). Next we can initiate a dictionary using dict and make a dictionary that has all the sides as keys with a value of 0. Now looks like this d = {1:0, 2:0...n+1:0}.From here now we can use a for loop to populate our dictionary adding 1 to whatever side is rolled. Another for loop will let us print out our dictionary. Bonus. I threw in a max function that takes the items in our dictionary and sorts them by their values and returns the largest tuple of (key, value). We can then print a most rolled statement.
from random import choice
rolls = int(input('Enter the amount of rolls: '))
sides = int(input('Enter the amound of sides: '))
die = list(range(1, sides+1))
d = dict((i,0) for i in die)
for i in range(rolls):
d[choice(die)] += 1
print('\nIn {} rolls, you rolled: '.format(rolls))
for i in d:
print('\tRolled {}: {} times'.format(i, d[i]))
big = max(d.items(), key=lambda x: x[1])
print('{} was rolled the most, for a total of {} times'.format(big[0], big[1]))
Enter the amount of rolls: 5
Enter the amound of sides: 5
In 5 rolls, you rolled:
Rolled 1: 1 times
Rolled 2: 2 times
Rolled 3: 1 times
Rolled 4: 1 times
Rolled 5: 0 times
2 was rolled the most, for a total of 2 times
from random import randint
numberOfDoors = 3
success = 0
attempts = 0
while True:
try:
doors = [0] * numberOfDoors
doors[randint(0, numberOfDoors - 1)] = 1
chosen = randint(0, numberOfDoors - 1)
while numberOfDoors > 2:
notIn = -1
while notIn == -1:
index = randint(0, numberOfDoors - 1)
if doors[index] == 0 and index != chosen:
notIn = index
if notIn < chosen:
chosen -= 1
del doors[notIn]
numberOfDoors -= 1
# doors is 2, so not chosen (0 or 1) will return the opposite (1 or 0)
success += doors[not chosen]
attempts += 1
if attempts % 1000000 == 0:
print float(success) / float(attempts)
except KeyboardInterrupt:
print float(success) / float(attempts)
break
My results are almost exactly 50% after a few hours of simulation - am I doing something specifically wrong?
Theoretically the door you choose is between 1/3 odds and 2/3 odds, so you should get higher than 50 at the very least.
This answer seems to do the same thing as me (ignoring that he doesn't do anything about monty's choice - I wanted to illustrate the concept).
You're forgetting to reset numberOfDoors (number of doors still closed, right?) back to 3. Since every iteration of the first while True: represents a new game show run, the show starts with all three doors initially closed.
...
while True:
numberOfDoors = 3
try:
doors = [0] * numberOfDoors
doors[randint(0, numberOfDoors - 1)] = 1
...
Next time, try adding print statements to help you debug. In this case, adding print doors right after you assign a car shows that doors has only two elements after the first iteration.
I wrote a Monty Hall simulation problem myself a while ago. Maybe it will help you with your code - in particular the comments may be useful:
from __future__ import division
import random
results = [] # a list containing the results of the simulations, either 'w' or 'l', win or lose
count = 0
while count <200: #number of simulations to perform
l = []
prize = random.randint(1, 3) #choose the prize door
initialchoice = random.randint(1, 3) #make an initial choice (the door the contestant chooses)
exposed = random.randint(1, 3) #choose the exposed door (the door the host chooses)
while exposed == initialchoice or exposed == prize: #make sure exposed is not same as prize or the initial choice
exposed = random.randint(1, 3)
if initialchoice != prize:
results.append('l') #if the initial choice was incorrect, append 'l'
else:
results.append('w') #if the initial choice was correct, append 'w'
count += 1
print 'prize door:', prize, 'initial choice:',initialchoice, 'exposed door:',exposed #print the results of the simulation
print
w = 0
for i in results:
if i == 'w':
w += 1
print w/len(results) #fraction of times sticking with the original door was successful