Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Unique Random Strings

I want to create a code that would generate number of random strings (for eg.10 in this case). I used the code below but the code kept on generating same strings

Input Code

import random
import string

s = string.ascii_lowercase
c = ''.join(random.choice(s) for i in range(8))

for x in range (0,10):
    print(c)

Output

ashjkaes
ashjkaes
ashjkaes
ashjkaes
ashjkaes
ashjkaes
ashjkaes
ashjkaes
press any key to continue .....

Please help

like image 423
Aryaj Pandey Avatar asked Oct 30 '25 06:10

Aryaj Pandey


1 Answers

What you do now is you generate a random string c then print it 10 times You should instead place the generation inside the loop to generate a new string every time.

import random
import string

s = string.ascii_lowercase

for i in range(10):
    c = ''.join(random.choice(s) for i in range(8))
    print(c)
like image 161
David Sidarous Avatar answered Nov 01 '25 19:11

David Sidarous