מספרים אקראיים בפייתון

Python מגדיר קבוצה של פונקציות המשמשות ליצירה או מניפולציה של מספרים אקראיים דרך מודול אקראי. 

פונקציות ב מודול אקראי להסתמך על פונקציה של מחולל מספרים פסאודו-אקראיים (random() אשר יוצרת מספר צף אקראי בין 0.0 ל-1.0. סוג מסוים של פונקציות זה משמש בהגרלות משחקים רבות או בכל יישום הדורש יצירת מספרים אקראי.

הבה נראה דוגמה ליצירת אקראי מִספָּר ב-Python באמצעות ה פונקציה random() .

Python
   import   random   num   =   random  .  random  ()   print  (  num  )   

תְפוּקָה:  

 0.30078080420602904  

דרכים שונות ליצור מספר אקראי בפייתון

ישנן מספר דרכים ליצור מספרים אקראיים ב-Python באמצעות הפונקציות של המודול האקראי של Python. הבה נראה כמה דרכים שונות.

יצירת מספר אקראי באמצעות choice()

פִּיתוֹן random.choice() היא פונקציה מובנית בשפת התכנות Python שמחזירה פריט אקראי מ-a רְשִׁימָה tuple אוֹ חוּט .

Python
   # import random   import   random   # prints a random value from the list   list1   =   [  1     2     3     4     5     6  ]   print  (  random  .  choice  (  list1  ))   # prints a random item from the string   string   =   'striver'   print  (  random  .  choice  (  string  ))   

תְפוּקָה:

 5   
t

יצירת מספר אקראי באמצעות randrange()

המודול האקראי מציע פונקציה שיכולה ליצור מספרים אקראיים של Python מטווח מוגדר וגם מאפשר מקום לכלול שלבים הנקראים randrange() .

Python
   # importing 'random' for random operations   import   random   # using choice() to generate a random number from a   # given list of numbers.   print  (  'A random number from list is : '     end  =  ''  )   print  (  random  .  choice  ([  1     4     8     10     3  ]))   # using randrange() to generate in range from 20   # to 50. The last parameter 3 is step size to skip   # three numbers when selecting.   print  (  'A random number from range is : '     end  =  ''  )   print  (  random  .  randrange  (  20     50     3  ))   

תְפוּקָה:  

 A random number from list is : 4   
A random number from range is : 41

יצירת מספר אקראי באמצעות seed()

פִּיתוֹן random.seed() הפונקציה משמשת לשמירת המצב של פונקציה אקראית כך שהיא תוכל ליצור מספרים אקראיים ב-Python על מספר ביצועים של הקוד על אותה מכונה או במכונות שונות (עבור ערך סיד ספציפי). ערך הזרע הוא מספר הערך הקודם שנוצר על ידי המחולל. בפעם הראשונה כאשר אין ערך קודם הוא משתמש בזמן המערכת הנוכחי.

Python
   # importing 'random' for random operations   import   random   # using random() to generate a random number   # between 0 and 1   print  (  'A random number between 0 and 1 is : '     end  =  ''  )   print  (  random  .  random  ())   # using seed() to seed a random number   random  .  seed  (  5  )   # printing mapped random number   print  (  'The mapped random number with 5 is : '     end  =  ''  )   print  (  random  .  random  ())   # using seed() to seed different random number   random  .  seed  (  7  )   # printing mapped random number   print  (  'The mapped random number with 7 is : '     end  =  ''  )   print  (  random  .  random  ())   # using seed() to seed to 5 again   random  .  seed  (  5  )   # printing mapped random number   print  (  'The mapped random number with 5 is : '     end  =  ''  )   print  (  random  .  random  ())   # using seed() to seed to 7 again   random  .  seed  (  7  )   # printing mapped random number   print  (  'The mapped random number with 7 is : '     end  =  ''  )   print  (  random  .  random  ())   

תְפוּקָה:  

 A random number between 0 and 1 is : 0.510721762520941   
The mapped random number with 5 is : 0.6229016948897019
The mapped random number with 7 is : 0.32383276483316237
The mapped random number with 5 is : 0.6229016948897019
The mapped random number with 7 is : 0.32383276483316237

יצירת מספר אקראי באמצעות shuffle()

ה לְעַרְבֵּב() הפונקציה משמשת לערבב רצף (רשימה). ערבוב פירושו שינוי המיקום של רכיבי הרצף. כאן פעולת הדשדוש במקום.

Python
   # import the random module   import   random   # declare a list   sample_list   =   [  'A'     'B'     'C'     'D'     'E'  ]   print  (  'Original list : '  )   print  (  sample_list  )   # first shuffle   random  .  shuffle  (  sample_list  )   print  (  '  n  After the first shuffle : '  )   print  (  sample_list  )   # second shuffle   random  .  shuffle  (  sample_list  )   print  (  '  n  After the second shuffle : '  )   print  (  sample_list  )   

תְפוּקָה:

 Original list :    
['A' 'B' 'C' 'D' 'E']

After the first shuffle :
['A' 'B' 'E' 'C' 'D']

After the second shuffle :
['C' 'E' 'B' 'D' 'A']

יצירת מספר אקראי באמצעות uniform()

ה אָחִיד() הפונקציה משמשת ליצירת מספר אקראי של Python בנקודה צפה בין המספרים המוזכרים בארגומנטים שלה. זה דורש שני ארגומנטים גבול תחתון (כלול בדור) וגבול עליון (לא כלול בדור).

Python
   # Python code to demonstrate the working of   # shuffle() and uniform()   # importing 'random' for random operations   import   random   # Initializing list   li   =   [  1     4     5     10     2  ]   # Printing list before shuffling   print  (  'The list before shuffling is : '     end  =  ''  )   for   i   in   range  (  0     len  (  li  )):   print  (  li  [  i  ]   end  =  ' '  )   print  (  '  r  '  )   # using shuffle() to shuffle the list   random  .  shuffle  (  li  )   # Printing list after shuffling   print  (  'The list after shuffling is : '     end  =  ''  )   for   i   in   range  (  0     len  (  li  )):   print  (  li  [  i  ]   end  =  ' '  )   print  (  '  r  '  )   # using uniform() to generate random floating number in range   # prints number between 5 and 10   print  (  'The random floating point number between 5 and 10 is : '     end  =  ''  )   print  (  random  .  uniform  (  5     10  ))   

תְפוּקָה:  

 The list before shuffling is : 1 4 5 10 2    
The list after shuffling is : 2 1 4 5 10
The random floating point number between 5 and 10 is : 5.183697823553464