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
#@author: www.jl-blog.com
#generate a random integer from 1 to 8
def random_func():
print('random intger from 1 to 8')
print(random.randint(1,8))
#generate a random float from 0 to 1
print('random float from 0 to 1')
print(random.random())
#generate a float in between 1.1 to 8.8
print('random float from 1.1 to 8.8')
print(random.uniform(1.1,8.8))
#generate a element from a sequence
print('generate a element from a sequence')
seq='JL-Blog'
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(random.randrange(1,88,8))
a=[1,3,5,6,7]
print('List a ->',a)
random.shuffle(a)
# reorder the element in list a
print('After shuffle ->',a)
def main():
random_func()
if __name__=="__main__":
main()
3. Running 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
#copyright: https://www.jl-blog.com/
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]
time.sleep(1)
print( "\nThe dices are {},{},{} respectively, It is a {}".format(
a, b, c, win_result))
time.sleep(1)
if choice == str(win_choice):
print("\nCongrats, you win the game!!")
elif choice != str(win_choice):
print("\nSorry, you lost. Please try again!")
else:
time.sleep(1)
print("Wrong input, game restarting...\n")
continue_game = input("\nDo you like one more game? (Y/N)").upper()
if continue_game == "Y":
time.sleep(1)
a, b, c, choice = game_init()
elif continue_game == "N":
print("\nThank you for playing. Have a wonderful day.")
break
else:
time.sleep(1)
print("Wrong input, game restarting...\n")
a, b, c, choice = game_init()
if __name__ == "__main__":
main()