# Generating Random String Using PHP

Generate a random, unique, alpha-numeric string using PHP.

Examples:

EA070 aBX32gTf

**APPROACH 1: Brute Force**

The first approach is the simplest one to understand and thus brute force.

It can be achieved as follows:

- Store all the possible letters into a string.
- Generate random index from 0 to string length-1.
- Print the letter at that index.
- Perform this step n times (where n is the length of string required).

**Program:**

`<?php ` `$n` `=10; ` `function` `getName(` `$n` `) { ` ` ` `$characters` `= ` `'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'` `; ` ` ` `$randomString` `= ` `''` `; ` ` ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) { ` ` ` `$index` `= rand(0, ` `strlen` `(` `$characters` `) - 1); ` ` ` `$randomString` `.= ` `$characters` `[` `$index` `]; ` ` ` `} ` ` ` ` ` `return` `$randomString` `; ` `} ` ` ` `echo` `getName(` `$n` `); ` `?> ` |

*chevron_right*

*filter_none*

Output 1:

3HDrSOvRIs

Output 2:

lipHh

**APPROACH 2: Using Hashing Functions**

PHP has a few functions like md5(), sha1() and hash(), that can be used to hash a string based on certain algorithms like “sha1”, “sha256”, “md5” etc. All these function takes a string as an argument and output an Alpha-Numeric hashed string.

To learn more about these functions click here.

Once we understand how we utilize these functions, our task becomes pretty simple.

- Generate a random number using rand() function.
- Hash it using one of the above functions.

**Program 1:**

`<?php ` `$str` `=rand(); ` `$result` `= md5(` `$str` `); ` `echo` `$result` `; ` `?> ` |

*chevron_right*

*filter_none*

Output 1:

2e437510c181dd2ae100fc1661a445d4

Output 2:

256394010059991a71ea05e5d859d2be

**Program 2:**

`<?php ` `$str` `=rand(); ` `$result` `= sha1(` `$str` `); ` `echo` `$result` `; ` `?> ` |

*chevron_right*

*filter_none*

Output 1:

6eadd9b2c4389d9b109b3b869f66aab5d8f9420a

Output 2:

ca2d3c0993ab87e842d0a7a01f319aca6c587a87

**Program 3:**

`<?php ` `$str` `= rand(); ` `$result` `= hash(` `"sha256"` `, ` `$str` `); ` `echo` `$result` `; ` `?> ` |

*chevron_right*

*filter_none*

Output 1:

2a41cbc8cc11f8c8d0eb54210fe524748b4def1c5b04fcf18c2d5972e24d11c2

Output 2:

291144c1cbba4de0bf199d37ee265ac95cc2e44e80fd2642b22a6e8ef2f42a39

**NOTE:** All the above functions are hashing functions, hence the length of the string generated will always depend on the algorithm used, but for an algorithm it will always remain constant. So if you want to generate string of a fixed length, you can either truncate the generated string or concatenate with another string, based on the requirement.

**Approach 3:Using uniqid() function.**

The uniqid( ) function in PHP is an inbuilt function which is used to generate a unique ID based on the current time in microseconds (micro time). By default, it returns a 13 character long unique string.

**Program:**

`<?php ` `$result` `= uniqid(); ` `echo` `$result` `; ` `?> ` |

*chevron_right*

*filter_none*

Output1:

5bdd0b74e9a6c

Output2:

5bdd0bbc200c4

**NOTE:** All the above approaches are built on rand() and uniqid() functions. These functions are not cryptographically secure random generators. So it is advised that if the degree of randomness affect the security of an application, these methods should be avoided.

**Approach 4: Using random_bytes() function. (Cryptographically Secure)**

The random_bytes() function generates cryptographically secure pseudo-random bytes, which can later be converted to hexadecimal format using bin2hex() function.

**Program:**

`<?php ` `$n` `= 20; ` `$result` `= bin2hex(random_bytes(` `$n` `)); ` `echo` `$result` `; ` `?> ` |

*chevron_right*

*filter_none*

Output1:

235aed08a01468f90fa726bd56319fb893967da8

Output2:

508b84494cdf31bec01566d12a924c75d4baed39

## Recommended Posts:

- Program to generate random string in PHP
- How to generate a random, unique, alphanumeric string in PHP
- Generating OTP (One time Password) in PHP
- Implement random-0-6-Generator using the given random-0-1-Generator
- How to get random value out of an array in PHP?
- QuickSort using Random Pivoting
- Generate a random permutation of 1 to N
- Binomial Random Variables
- Random Walk (Implementation in Python)
- Clone a Binary Tree with Random Pointers
- PHP program to Generate the random number in the given range (min, max)
- Select a random number from stream, with O(1) space
- Select a Random Node from a tree with equal probability
- Select a Random Node from a Singly Linked List
- Random number generator in arbitrary probability distribution fashion

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.