Nejauši skaitļi Python

Python definē funkciju kopu, ko izmanto, lai ģenerētu vai manipulētu ar nejaušiem skaitļiem, izmantojot izlases modulis. 

Funkcijas izlases modulis paļauties uz pseidogadījuma skaitļu ģeneratora funkciju random(), kas ģenerē nejaušu peldošo skaitli no 0,0 līdz 1,0. Šīs īpašās funkcijas tiek izmantotas daudzās spēļu loterijās vai jebkurā lietojumprogrammā, kurā nepieciešama nejaušu skaitļu ģenerēšana.

Apskatīsim nejaušības ģenerēšanas piemēru numuru Python, izmantojot izlases() funkcija.

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

Izvade:  

 0.30078080420602904  

Dažādi veidi, kā ģenerēt izlases skaitļus programmā Python

Ir vairāki veidi, kā Python ģenerēt nejaušus skaitļus, izmantojot Python izlases moduļa funkcijas. Apskatīsim dažus dažādus veidus.

Izlases skaitļa ģenerēšana izmantojot izvēli ()

Python random.choice() ir Python programmēšanas valodā iebūvēta funkcija, kas atgriež nejaušu vienumu no a sarakstu korts vai virkne .

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  ))   

Izvade:

 5   
t

Nejauša skaitļa ģenerēšana, izmantojot randrange()

Nejaušais modulis piedāvā funkciju, kas var ģenerēt Python nejaušus skaitļus no noteikta diapazona, kā arī ļauj iekļaut soļus, ko sauc par 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  ))   

Izvade:  

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

Nejauša skaitļa ģenerēšana, izmantojot seed()

Python random.seed() Funkciju izmanto, lai saglabātu nejaušas funkcijas stāvokli, lai tā varētu ģenerēt dažus nejaušus skaitļus Python, vairākkārt izpildot kodu tajā pašā mašīnā vai dažādās iekārtās (konkrētai sākuma vērtībai). Sākotnējā vērtība ir iepriekšējās vērtības numurs, ko ģenerējis ģenerators. Pirmo reizi, kad nav iepriekšējās vērtības, tiek izmantots pašreizējais sistēmas laiks.

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  ())   

Izvade:  

 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

Nejauma skaitļa ģenerēšana, izmantojot shuffle()

The jaukt () funkcija tiek izmantota, lai jauktu secību (sarakstu). Sajaukšana nozīmē secības elementu pozīcijas maiņu. Šeit notiek jaukšanas darbība.

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  )   

Izvade:

 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']

Nejauša skaitļa ģenerēšana, izmantojot uniform()

The uniforma () funkcija tiek izmantota, lai ģenerētu peldošā komata Python izlases skaitli starp skaitļiem, kas minēti tās argumentos. Tam nepieciešami divi argumenti: apakšējā robeža (iekļauta paaudzē) un augšējā robeža (nav iekļauta paaudzē).

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  ))   

Izvade:  

 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