Question

PYTHON: Dice Rolling Write a script that takes input for the number of dice to roll...

PYTHON: Dice Rolling

  1. Write a script that takes input for the number of dice to roll and for the number of sides for the dice. Roll that many n-sided dice and store the values an array. Take the number of sides of the dice and the number of dice as typed inputs from the user. Output should be a comma separated list of the die numbers with no spaces like this:
    6,4,1,3,1
  2. Write a script that will simulate the roll of an UNFAIR 6-sided die. Instead of each side having an even chance of coming up (1/6 = 16.7%), the middle numbers should be favored. There should be a 20% chance of rolling a 2, 3, 4, or 5, and only a 10% chance of rolling a 1 or a 6. Hint: You will need the round function and some other simple math. This is hard because of the thinking involved, not because of the coding; in fact, you can write it in one (sort of complex) line of code.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Please find your answers below. Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.


First script


import random

#reading number of rolls and number of sides 
num_rolls=int(input("Please enter the number of dice to roll: "))
sides=int(input("Please enter the number of sides for a dice: "))

#creating an empty list
rolls=[]

#looping for num_rolls number of times
for i in range(num_rolls):
    #generating a value between 1 and sides
    roll=random.randint(1,sides)
    #appending to list
    rolls.append(roll)

#printing the contents of rolls list comma separated
for i in range(num_rolls):
    print(rolls[i],end='') #printing roll, followed by no line break
    #printing a comma if this is not the last element
    if i<num_rolls-1:
        print(',',end='')
print() #line break

Second script

import random


#generating a random number between 1 and 100 (percentage), returning 1 or 6 (equal probability) if the generated
#number is less than or equal to 20 (20%, so 10% each for 1 and 6), otherwise returning a random value from [2,3,4,5]
roll = random.choice([1, 6]) if random.randint(1, 100) <= 20 else random.choice([2, 3, 4, 5])

#printing roll
print(roll)

#end of script


Note: There are several ways of implementing second question that I can think of. For example, below line will also return the unfair dice roll, giving 20% probability each for 2,3,4 and 5, 10% probability for 1 and 6.

roll = random.choice([1,2,3,4,5,6,2,3,4,5])

What we did was making the 6 sided dice a 10 sided one, by adding duplicate of sides 2,3,4 and 5, so probability of choosing a number from the list is 10% by default. But since 2,3,4,5 appear twice, their probabilities become 10+10=20%

You can also use numpy if it is allowed to generate random values based on given weighted probabilities. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Add a comment
Know the answer?
Add Answer to:
PYTHON: Dice Rolling Write a script that takes input for the number of dice to roll...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT