PYTHON:  From Random Module To The Dice Game

Spread the love

1. Random Module

In python, the function random() can not be directly called. However, we need to import a random module and generate random numbers by calling the static method. By importing this random module, it implements pseudo-random number generators for various distributions.

2. Random Function Details

import random
#generate a random integer from 1 to 8
def random_func():
	print('random intger from 1 to 8')
	#generate a random float from 0 to 1
	print('random float from 0 to 1')
	#generate a float in between 1.1 to 8.8
	print('random float from 1.1 to 8.8')
	#generate a element from a sequence
	print('generate a element from a sequence')
	print('Random elment from {seq}', random.choice('JLblog'))   
	#Generate a random integers in the interval of 8 from 1 to 88
	print('Generate a random integers in the interval of 8 from 1 to 88')
	print('List a ->',a)
	# reorder the element in list a 
	print('After shuffle ->',a)

def main():

if __name__=="__main__":

3. Running Result

random code result

4. Ancient Dice Game Introduction


​ 骰宝是一种古老的中国骰子游戏,在古代尤为盛行。玩家押注三个在笼子中转动的骰子的旋转结果。您可以同时押注在桌面上的一个或多个数字。游戏的牌桌分割成几部分押注“区域”,每一个区域代表不同类的骰子结果或组合。

游戏开始时 ,放置筹码在桌面上任何地方代表一个或多个骰子组合。下注完成后,装有骰子的笼子将开始旋转, 结果将显示在屏幕的右上方。





Game rule translation:

In ancient China, it was one of the most popular games in casinos. To start the game, there are three cups covering the dice on the table. The player can gamble for the sum of three rotating dice under the cup. Before the game starts, players guess the sum of the dice.

There are three kinds of results that could be generated:

  • 「Small」The sum of three dices are from 4 to 10.
  • 「Big」The sum of three dices are from 11 to 17.
  • 「Full Circle」Three dices are the same.

5. Dice Game Source Code

import random
import time

def game_init():
    # init 3 dice into var a,b,c
    a = random.randint(1, 6)
    b = random.randint(1, 6)
    c = random.randint(1, 6)
    print("---------- Ancient Dice Game ----------")
    print("Rules are as showing below:")
    print("(1)「Small」 The sum of three dices are from 4 to 10. ")
    print("(2)「Big」 The sum of three dices are from 11 to 17.")
    print("(3)「Full Circle」 Three dices are the same.  ")
    print("(4) Exit the game. ")
    choice = input("\nWhat is your choice(1,2,3,4): ")
    if choice == "4":
        print("Have a wonderful day!")
    return a, b, c, choice

def result_check(a, b, c):
    sum = a + b + c
    if sum >= 4 and sum <= 10:
        return [1, "「Small」"]
    elif sum <= 17 and sum >= 11 and a != b != c:
        return [2, "「Big」"]
    elif a == b == c:
        return [3, "「Full Circle」"]

def main():
    a, b, c, choice = game_init()

    while choice != "4":
        result = result_check(a, b, c)
        a, b, c = str(a), str(b), str(c)
        win_result = result[1]
        win_choice = result[0]
        print( "\nThe dices are {},{},{} respectively, It is a {}".format(
                a, b, c, win_result))
        if choice == str(win_choice):
            print("\nCongrats, you win the game!!")
        elif choice != str(win_choice):
            print("\nSorry, you lost. Please try again!")
            print("Wrong input, game restarting...\n")
        continue_game = input("\nDo you like one more game? (Y/N)").upper()
        if continue_game == "Y":
            a, b, c, choice = game_init()
        elif continue_game == "N":
            print("\nThank you for playing. Have a wonderful day.")
            print("Wrong input, game restarting...\n")
            a, b, c, choice = game_init()

if __name__ == "__main__":

6. Dice Game Running Result

dice game result
If you want to know more about me, please get on the about page. :)
Posts created 18

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
error: Content is protected !!