sample() function သည် random moduel ထဲမှ inbuilt function တစ်ခုဖြစ်သည်။ ပေးထားသည့် ဒေတာ sequence တစ်ခုမှ item များကို မိမိအလိုရှိသည့် အရေအတွက်အတိုင်း randomly ရွေးချယ်ပေးသည်။ ဒေတာ sequence သည် list တစ်ခုသော်လည်းကောင်း, tuple တစ်ခုသော်လည်းကောင်း, string တစ်ခုသော်လည်းကောင်း, set တစ်ခုသော်လည်းကောင်း ဖြစ်နိုင်သည်။
Syntax : random.sample(sequence, k) sample() function ၏ လက်သည်းကွင်းထဲသို့ sequence နှင့် မိမိအလိုရှိသည့် အရေအတွက် k ကို ထည့်ပေးရသည်။
ထူးခြားချက်မှာ sequence ထဲမှ duplicate item များ ပေးလိမ့်မည် မဟုတ်သောကြောင့် အရေအတွက် k သည် sequence အရေအတွက်ထက် နည်းရမည်။ တူညီနိုင်သည်။
ဥပမာ ရန်ကုန် မန္တလေး မေမြို့ ရွေဘို စသည့် မြို့ (၄)မြို့ မှ (၂)မြို့ ကို randomly ရွေးချယ်ပေးသည့် ဥပမာ ဖြစ်သည်။
import random
mylist = ["Yangon", "Mandalay", "Maymyo", "Shaw Bo"]
print(random.sample(mylist, k=2))
# List တစ်ခုမှ random sample ထုတ်ယူခြင်း
list1 = [1, 2, 3, 4, 5, 6]
print("random sample from given list:", random.sample(list1, 3))
# string တစ်ခုမှ random sample ထုတ်ယူခြင်း
string = "Union of Myanmar"
print("random sample from given string:", random.sample(string, 4))
# tuple တစ်ခုမှ random sample ထုတ်ယူခြင်း
tuple1 = ("ankit", "geeks", "computer", "science",
"portal", "scientist", "btech")
print("random sample from given tuple:", random.sample(tuple1, 4))
# set တစ်ခုမှ random sample ထုတ်ယူခြင်း
set1 = {"a", "b", "c", "d", "e"}
print("random sample from given set:", random.sample(set1, 3))
shuffle() method သည် ဒေတာ sequence တစ်ခုမှ item များ၏ အစဥ်(order)ကို ပြန်စဥ်(reorganize)ပေးသည်။ ဒေတာ sequence သည် list တစ်ခုသော်လည်းကောင်း, tuple တစ်ခုသော်လည်းကောင်း,set တစ်ခုသော်လည်းကောင်း ဖြစ်နိုင်သည်။ ထည့်ပေးသည့် အရေအတွက် item များ၏ အရေအတွက်အတိုင်းသာ ဖြစ်သည်။
random.shuffle(sequence, function)
mylist = ["apple", "banana", "cherry"]
random.shuffle(mylist)
print(mylist)
# list အသစ်တစ်ခု မဖြစ်ပေါ်ပါ။ မူလ list မှာ item များကို ရှေ့နောက် ပြောင်းပေးခြင်း ဖြစ်သည်။
number_list = [7, 14, 21, 28, 35, 42, 49, 56, 63, 70]
print("Original list:", number_list)
random.shuffle(number_list)
print("List after first shuffle:", number_list)
random.shuffle(number_list)
print("List after second shuffle:", number_list)
Password generator တစ်ခု ပြလုပ်ပုံကို ဥပမာ အဖြစ် ပြထားသည်။ အကြီးစာလုံး နှင့် အသေးစာလုံးများပါသော အင်္ဂလိပ် အက္ခရာများထဲမှာ (၅)လုံးပါရမည်။ ကိန်းဂဏန်း(digit) များထဲမှာ (၃)လုံးပါရမည်။ symbol များထဲမှာ (၂)လုံးပါရမည်။
## Method One
import random
letter = [ "a", "b", "c", "d", "e", "f", "g", "A", "B", "C", "D", "E", "F", "G"]
digit = [ "0", "1", "2", "3", "4" , "5", "6", "7", "8", "9"]
symbol = [ "!", "$", "%", "@", "&" , "^", "(", ")"]
# random.sample() ဖြင့် မိမိအလိုရှိသည့် အရေအတွက် အတိုင်း တစ်မျိုးခြင်းစီ ထုတ်ယူပြီး ပေါင်းသည်။
password_raw = (random.sample(letter, 5)) + (random.sample(digit, 3)) + (random.sample(symbol, 2))
print(f"Method One: {password_raw} ")
# random.sample() ဖြင့်ပင် ထုတ်ယူထားသည့် ၁၀ လုံးကို ရှေ့နောက်ပြန်စဥ်သည်။
password_1 = random.sample(password_raw , 10)
#.join() ဖြင့် တစ်လုံးချင်းစီကို ပြန်ပေါင်းသည်။
password_final = ''.join(password_1 )
print(f"Password generated by Method One: {password_final}")
## Method Two
password_2 = password_raw = (random.sample(letter, 5)) + (random.sample(digit, 3)) + (random.sample(symbol, 2))
print(f"Method Two: {password_2} ")
# random.shuffle() ဖြင့်ပင် ထုတ်ယူထားသည့် ၁၀ လုံးကို ရှေ့နောက်ပြန်စဥ်သည်။
random.shuffle(password_2)
password = ''.join(password_2)
print(f"Password generated by Method Two: {password} ")