(Count single digits) Write a program that generates 1,000 random integers between 0 and 9 and displays the count for each number. (Hint: Use a list of ten integers, say counts, to store the counts for the number of 0s, 1s, ..., 9s.)
Program code:
import random
# Initialize a list of counts for each number from 0 to 9
counts = [0] * 10
# Generate 1,000 random integers between 0 and 9
for _ in range(1000):
number = random.randint(0, 9)
counts[number] += 1
# Display the count for each number
for i in range(10):
print(f"Count of {i}: {counts[i]}")
Output of the program code:
Output 1:
Count of 0: 94
Count of 1: 121
Count of 2: 93
Count of 3: 90
Count of 4: 96
Count of 5: 103
Count of 6: 117
Count of 7: 81
Count of 8: 108
Count of 9: 97
Output 2:
Count of 0: 94
Count of 1: 121
Count of 2: 93
Count of 3: 90
Count of 4: 96
Count of 5: 103
Count of 6: 117
Count of 7: 81
Count of 8: 108
Count of 9: 97