logo

Program Python za ustvarjanje naključnega niza

Naključno se nanaša na zbirko podatkov ali informacij, ki so lahko na voljo v poljubnem vrstnem redu. The naključen modul v pythonu se uporablja za ustvarjanje naključnih nizov. Naključni niz je sestavljen iz številk, znakov in niza ločil, ki lahko vsebujejo poljuben vzorec. Naključni modul vsebuje dve metodi random.choice() in secrets.choice() , da ustvarite varen niz. Razumejmo, kako ustvariti naključni niz z uporabo metod random.choice() in secrets.choice() v python .

Program Python za ustvarjanje naključnega niza

Uporaba random.choice()

The random.choice() funkcija se uporablja v nizu python za ustvarjanje zaporedja znakov in števk, ki lahko ponavljajo niz v poljubnem vrstnem redu.

Ustvarite program za ustvarjanje naključnega niza s funkcijo random.choices().

random_str.py

 import string import random # define the random module S = 10 # number of characters in the string. # call random.choices() string module to find the string in Uppercase + numeric data. ran = ''.join(random.choices(string.ascii_uppercase + string.digits, k = S)) print('The randomly generated string is : ' + str(ran)) # print the random data 

Izhod:

Program Python za ustvarjanje naključnega niza

Sledi metoda, uporabljena v naključnem modulu za ustvarjanje naključnega niza.

Metode Opis
Niz.ascii_črke Vrne naključni niz, ki vsebuje tako velike kot male črke.
String_ascii_uppercase Je metoda naključnega niza, ki vrne samo niz z velikimi črkami.
String.ascii_lowercase Je metoda naključnega niza, ki vrne niz samo z malimi črkami.
String.digits Je metoda naključnega niza, ki vrne niz s številskimi znaki.
Niz.ločila Je metoda naključnega niza, ki vrne niz z ločili.

Ustvarite naključni niz velikih in malih črk

UprLwr.py

 # write a program to generate the random string in upper and lower case letters. import random import string def Upper_Lower_string(length): # define the function and pass the length as argument # Print the string in Lowercase result = ''.join((random.choice(string.ascii_lowercase) for x in range(length))) # run loop until the define length print(' Random string generated in Lowercase: ', result) # Print the string in Uppercase result1 = ''.join((random.choice(string.ascii_uppercase) for x in range(length))) # run the loop until the define length print(' Random string generated in Uppercase: ', result1) Upper_Lower_string(10) # define the length 

Izhod:

Program Python za ustvarjanje naključnega niza

Naključni niz določenih znakov

Specific.py

 # create a program to generate the random string of given letters. import random import string def specific_string(length): sample_string = 'pqrstuvwxy' # define the specific string # define the condition for random string result = ''.join((random.choice(sample_string)) for x in range(length)) print(' Randomly generated string is: ', result) specific_string(8) # define the length specific_string(10) 

Izhod:

Program Python za ustvarjanje naključnega niza

Opomba: metoda random.choice() se v programu python uporablja za ponavljanje nizov istih znakov. Če ne želimo prikazati ponavljajočih se znakov, uporabimo funkcijo random.sample().

Ustvarite naključni niz brez ponavljanja istih znakov

WithoutRepeat.py

 # create a program to generate a string with or without repeating the characters. import random import string print('Use of random.choice() method') def specific_string(length): letters = string.ascii_lowercase # define the lower case string # define the condition for random.choice() method result = ''.join((random.choice(letters)) for x in range(length)) print(' Random generated string with repetition: ', result) specific_string(8) # define the length specific_string(10) print('') # print the space print('Use of random.sample() method') def WithoutRepeat(length): letters = string.ascii_lowercase # define the specific string # define the condition for random.sample() method result1 = ''.join((random.sample(letters, length))) print(' Random generated string without repetition: ', result1) WithoutRepeat(8) # define the length WithoutRepeat(10) 

Izhod:

Program Python za ustvarjanje naključnega niza

Kot lahko vidimo v zgornjem izhodu, metoda random.sample() vrne niz, v katerem so vsi znaki edinstveni in se ne ponavljajo. Medtem ko metoda random.choice() vrne niz, ki lahko vsebuje ponavljajoče se znake. Torej lahko rečemo, da če želimo ustvariti edinstven naključni niz, uporabimo naključni vzorec () metoda.

Ustvarite naključni alfanumerični niz, sestavljen iz fiksnih črk in števk

Denimo, da želimo naključno generiran alfanumerični niz, ki vsebuje pet črk in štiri števke. Te parametre moramo definirati v funkciji.

bfs iskanje

Napišimo program za generiranje alfanumeričnega niza, ki vsebuje določeno število črk in števk.

fixedString.py

 import random import string def random_string(letter_count, digit_count): str1 = ''.join((random.choice(string.ascii_letters) for x in range(letter_count))) str1 += ''.join((random.choice(string.digits) for x in range(digit_count))) sam_list = list(str1) # it converts the string to list. random.shuffle(sam_list) # It uses a random.shuffle() function to shuffle the string. final_string = ''.join(sam_list) return final_string # define the length of the letter is eight and digits is four print('Generated random string of first string is:', random_string(8, 4)) # define the length of the letter is seven and digits is five print('Generated random string of second string is:', random_string(7, 5)) 

Izhod:

Program Python za ustvarjanje naključnega niza

Uporaba secrets.choice()

Metoda secrets.choice() se uporablja za ustvarjanje bolj varnega naključnega niza kot random.choice(). Je kriptografsko naključni generator nizov, ki zagotavlja, da dva procesa ne moreta istočasno pridobiti istih rezultatov z uporabo metode secrets.choice().

Napišimo program za tiskanje varnega naključnega niza z uporabo metode secrets.choice.

Secret_str.py

 import random import string import secrets # import package num = 10 # define the length of the string # define the secrets.choice() method and pass the string.ascii_letters + string.digits as an parameters. res = ''.join(secrets.choice(string.ascii_letters + string.digits) for x in range(num)) # print the Secure string print('Secure random string is :'+ str(res)) 

Izhod:

Program Python za ustvarjanje naključnega niza

Za ustvarjanje varnega naključnega niza uporabite drugo metodo naključnega modula.

Napišimo program za tiskanje varnih naključnih nizov z uporabo različnih metod secrets.choice().

Secret.py

 # write a program to display the different random string method using the secrets.choice(). # imports necessary packages import random import string import secrets num = 10 # define the length of the string # define the secrets.choice() method and pass the string.ascii_letters + string.digits as an parameters. res = ''.join(secrets.choice(string.ascii_letters + string.digits) for x in range(num)) # Print the Secure string with the combination of ascii letters and digits print('Secure random string is :'+ str(res)) res = ''.join(secrets.choice(string.ascii_letters) for x in range(num)) # Print the Secure string with the combination of ascii letters print('Secure random string is :'+ str(res)) res = ''.join(secrets.choice(string.ascii_uppercase) for x in range(num)) # Print the Secure string in Uppercase print('Secure random string is :'+ str(res)) res = ''.join(secrets.choice(string.ascii_lowercase) for x in range(num)) # Print the Secure string in Lowercase print('Secure random string is :'+ str(res)) res = ''.join(secrets.choice(string.ascii_letters + string.punctuation) for x in range(num)) # Print the Secure string with the combination of letters and punctuation print('Secure random string is :'+ str(res)) res = ''.join(secrets.choice(string.digits) for x in range(num)) # Print the Secure string using string.digits print('Secure random string is :'+ str(res)) res = ''.join(secrets.choice(string.ascii_letters + string.digits + string.punctuation) for x in range(num)) # Print the Secure string with the combonation of letters, digits and punctuation print('Secure random string is :'+ str(res)) 

Izhod:

Program Python za ustvarjanje naključnega niza