Generating random Id’s in Python

In python there are different ways to generate id’s. Let’s see how different types of Id’s can be generated using python without using inbuilt Python libraries.

1. Generating Random Integer as Ids’

Code #1 : Print 10 random values of numbers between 1 and 100.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate the
# random generation of Integer id's
  
import random
  
# determines how many values
# will be printed
for x in range(10):
      
    # print 10 random values
    # between 1 and 100
    print (random.randint(1, 101))

chevron_right


Output :

76
72
7
78
77
19
24
23
77
96

 
Code #2 : Print random numbers between 1 and 100 which are multiple of 5.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# the random generation of id's
# which are multilpe of 5
  
import random
  
# determines how many
# values will be printed
for x in range(10):
          
    # print 10 random values between
    # 1 and 100 which are multiple of 5
    print (random.randint(1, 20) * 5)

chevron_right


Output :



60
30
35
100
85
25
100
20
90
85

 

Drawbacks :

  • Generating Random numbers is not unique, Same number can repeat itself.
  • It generates only Integer values.
  • 2. Generating Random String as Ids’

    Generating Random string id’s consists of letters and digits. This can be useful in generating passwords as its provide the encryption and decryption technique.

    Code #1 : Show how to generate random string id’s.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python3 code to demonstrate the
    # random generation of string id's
      
    import random
    import string
      
    # Generate a random string
    # with 32 characters.
    random = ''.join([random.choice(string.ascii_letters
                + string.digits) for n in range(32)])
      
    # print the random
    # string of length 32
    print (random)

    chevron_right

    
    

    Output :

    Rf2IdqUNkURNN6mw82kSpyxQe9ib3usX
    

     
    Code #2 : Using Function call

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python3 code to demonstrate
    # the random generation of string id's
      
    import random
    import string
      
    # defining function for random
    # string id with parameter
    def ran_gen(size, chars=string.ascii_uppercase + string.digits):
        return ''.join(random.choice(chars) for x in range(size))
      
    # function call for random string
    # generation with size 8 and string 
    print (ran_gen(8, "AEIOSUMA23"))

    chevron_right

    
    

    Output :

    S2M2IEAO
    

    Generating Random id’s using UUID in Python



    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.