مشخصات مقاله
-
0.0
-
2234
-
0
-
0
ماژول random در پایتون
ماژول random در پایتون یک ماژول درونی است که برای تولید اعداد تصادفی در پایتون استفاده میشود. این اعداد به صورت تصادفی ایجاد میشوند و هیچ قوانین یا دستورات خاصی را دنبال نمیکنند. بنابراین، میتوانیم از این ماژول برای تولید اعداد تصادفی، نمایش یک مورد تصادفی از یک لیست یا رشته، و موارد مشابه استفاده کنیم.
تابع ()randomدر پایتون
تابع ()random.random یک عدد اعشاری ارائه میدهد که در محدوده 0.0 تا 1.0 قرار دارد. برای این تابع هیچ پارامتری لازم نیست. این متد مقدار دوم اعشاری تصادفی را در محدوده [0.0 و 1] برمیگرداند.
کد :# Python program for generating random float number import random num=random.random() print(num)خروجی :
0.3232640977876686
تابع ()randint در پایتون
تابع ()random.randint یک عدد صحیح تصادفی از محدودهای از اعداد تولید میکند که به عنوان ورودی به آن داده شده است.
کد :# Python program for generating a random integer import random num = random.randint(1, 500) print( num )خروجی :
215
تابع ()randrange در پایتون
تابع ()random.randrange یک مورد از محدودهای که توسط پارامترهای شروع (start)، پایان (stop) و گام (step) تعیین شده است، به صورت تصادفی انتخاب میکند. به طور پیشفرض، مقدار شروع به 0 تنظیم شده است. به همین ترتیب، مقدار گام نیز به صورت پیشفرض به 1 تنظیم شده است.
کد :# To generate value between a specific range import random num = random.randrange(1, 10) print( num ) num = random.randrange(1, 10, 2) print( num )خروجی :
4 9
تابع ()choice در پایتون
تابع ()random.choice یک مورد از یک سری غیر خالی به صورت تصادفی انتخاب میکند. در برنامه زیر دادهای به صورت یک رشته (string)، یک لیست (list) و یک مجموعه (set) تعریف شده است. و با استفاده از متد choice() فوق، یک عنصر تصادفی انتخاب میشود.
کد :
# To select a random element
import random
random_s = random.choice('Random Module') #a string
print( random_s )
random_l = random.choice([23, 54, 765, 23, 45, 45]) #a list
print( random_l )
random_s = random.choice((12, 64, 23, 54, 34)) #a set
print( random_s )
خروجی :
M 765 54
تابع ()shuffle در پایتون
تابع ()random.shuffle لیست داده شده را به صورت تصادفی مرتب میکند.
کد :# To shuffle elements in the list list1 = [34, 23, 65, 86, 23, 43] random.shuffle( list1 ) print( list1 ) random.shuffle( list1 ) print( list1 )خروجی :
[23, 43, 86, 65, 34, 23] [65, 23, 86, 23, 34, 43]
بازی سنگ کاغذ قیچی با ماژول random
# import random module
import random
# Function to play game
def start_game():
# Print games rules and instructions
print(" This is Javatpoint's Rock-Paper-Scissors! ")
print(" Please Enter your choice: ")
print(" choice 1: Rock ")
print(" choice 2: Paper ")
print(" choice 3: Scissors ")
#To take the user input
choice_user = int(input(" Select any options from 1 - 3 : "))
# randint() Function which generates a random number by computer
choice_machine = random.randint(1, 3)
# display the machines choice
print(" Option choosed by Machine is: ", end = " ")
if choice_machine == 1:
print(" Rock ")
elif choice_machine == 2:
print("Paper")
else:
print("Scissors")
# To declare who the winner is
if choice_user == choice_machine:
print(" Wow It's a tie! ")
elif choice_user == 1 and choice_machine == 3:
print(" Congratulations!! You won! ")
elif choice_user == 2 and choice_machine == 1:
print(" Congratulations!! You won! ")
elif choice_user == 3 and choice_machine == 2:
print(" Congratulations!! You won! ")
else:
print(" Sorry! The Machine Won the Game? ")
# If user wants to play again
play_again = input(" Want to Play again? ( yes / no ) ").lower()
if play_again == " yes ":
start_game()
else:
print(" Thanks for playing Rock-Paper-Scissors! ")
# Begin the game
start_game()
خروجی :
This is Javatpoint's Rock-Paper-Scissors! Please Enter your choice: choice 1: Rock choice 2: Paper choice 3: Scissors Select any options from 1 - 3 : 1 Option choosed by Machine is: Rock Wow It's a tie! Want to Play again? ( yes / no ) yes This is Javatpoint's Rock-Paper-Scissors! Please Enter your choice: choice 1: Rock choice 2: Paper choice 3: Scissors Select any options from 1 - 3 : 2 Option choosed by Machine is: Scissors Congratulations!! You won! Want to Play again? ( yes / no ) no Thanks for playing Rock-Paper-Scissors!