# Python | Program to generate one-time password (OTP)

One-time Passwords (OTP) is a password that is valid for only one login session or transaction in a computer or a digital device. Now a days OTP’s are used in almost every service like Internet Banking, online transactions etc. They are generally combination of 4 or 6 numeric digits or a 6-digit alphanumeric.

random() function can be used to generate random OTP which is predefined in random library. Let’s see how to generate OTP using Python.

Used Function:

random.random(): This function returns any random number between 0 to 1.
math.floor(): It returns floor of any floating number to a integer value.

Using the above function pick random index of string array which contains all the possible candidates of a particular digit of the OTP.

Example #1 : Generate 4 digit Numeric OTP

 `# import library ` `import` `math, random ` ` `  `# function to generate OTP ` `def` `generateOTP() : ` ` `  `    ``# Declare a digits variable   ` `    ``# which stores all digits  ` `    ``digits ``=` `"0123456789"` `    ``OTP ``=` `"" ` ` `  `   ``# length of password can be chaged ` `   ``# by changing value in range ` `    ``for` `i ``in` `range``(``4``) : ` `        ``OTP ``+``=` `digits[math.floor(random.random() ``*` `10``)] ` ` `  `    ``return` `OTP ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"` `: ` `     `  `    ``print``(``"OTP of 4 digits:"``, generateOTP()) `

Output:

```OTP of 4 digits: 3211
```

Example #2: Generate alphanumeric OTP of length 6

 `# import library ` `import` `math, random ` ` `  `# function to generate OTP ` `def` `generateOTP() : ` ` `  `    ``# Declare a string variable   ` `    ``# which stores all string  ` `    ``string ``=` `'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'` `    ``OTP ``=` `"" ` `    ``length ``=` `len``(string) ` `    ``for` `i ``in` `range``(``6``) : ` `        ``OTP ``+``=` `string[math.floor(random.random() ``*` `length)] ` ` `  `    ``return` `OTP ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"` `: ` `     `  `    ``print``(``"OTP of length 6:"``, generateOTP()) `

Output:

```OTP of length 6: pyelJl
```

Example #3: Using String constants

 `# Importing random to generate  ` `# random string sequence  ` `import` `random  ` `    `  `# Importing string library function  ` `import` `string  ` `    `  `def` `rand_pass(size):  ` `        `  `    ``# Takes random choices from  ` `    ``# ascii_letters and digits  ` `    ``generate_pass ``=` `''.join([random.choice( string.ascii_uppercase ``+` `                                            ``string.ascii_lowercase ``+` `                                            ``string.digits)  ` `                                            ``for` `n ``in` `range``(size)])  ` `                            `  `    ``return` `generate_pass  ` `    `  `# Driver Code   ` `password ``=` `rand_pass(``10``)  ` `print``(password)  `

Output:

`2R8gaoDKqn`

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 :

4

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