Write a program that will simulate the process of dealing cards

Question

Write a program that will simulate the process of dealing cards from a 52-card deck by generating 1,000 random integers in the range 1-52.

Assume that numbers 1-13 represent clubs, 14-26 represent diamonds, 27-39 represent hearts, and 40-52 represent spades. Display the number of times each suit occurred in the 1,000 “deals.”

 

Summary

In this question, we have provided a program and we have to write the python code and also the pseudocode for it. For the given scenario, we have explained the python code and the pseudocode in detail under explanation. Firstly, we have taken 1000 random numbers between 1 and 52, which represent the deck of cards. We have given particular ranges for each suit. Among 1000 random numbers how many times each suit number obtained is counted and at last, the count of all the suits are displayed as output to the console.

 

Explanation

We have declared the variables with the names clubs, diamonds, hearts, and spades and assigned them as zero, for having the count of each kind of suit. Then using the predefined random function, 1000 random integers are generated using randInt function of the random class. Then, it is checked if the range of each number is either 1 to 13, or 14 to 26, or 27 to 39, or 40 to 52. Based on the range, that particular suit count is incremented. Finally, all the suit counts are displayed as output to the console.

Pseudocode:

suitCount():
clubs <- 0
diamonds <- 0
hearts <- 0
spades <- 0
generate 1000 random numbers between 1 and 52
for every randomInteger of 1000 integers:
if randomInteger in the range 1 to 13:
then increment clubs
if the randomInteger is in the range 14 to 26:
then increment diamonds
if the randomInteger is in the range 27 to 39:
then increment hearts
if the randomInteger is in the range 40 to 52:
then increment spades

print clubs
and print diamonds
print hearts
and print spades

Code

import random 
# initially, count of each suit is 0 
clubs=0
diamonds=0 
hearts=0 
spades=0 
# to generate 1000 random numbers and 
# check whihc suit was obtained 
for i in range(1000):
    r=random.randint(1,52)
    # clubs range 
    if r>=1 and r<=14:
        clubs+=1 
    # diamonds range 
    elif r>=14 and r<=26:
        diamonds+=1 
    # hearts range 
    elif r>=27 and r<=39:
        hearts+=1 
    # spades range
    elif r>=40 and r<=52:
        spades+=1 
# displaying all the suits count obtained out of 1000 
print("Count of each suit:")
print("Clubs:",clubs)
print("Diamonds:",diamonds)
print("Hearts:",hearts)
print("Spades:",spades)

Output

 

process of dealing cards random number generator output

 

Also read, row_echelon_form method using elementary operator

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *