Program to generate random string in PHP Last Updated : 18 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Given a size N and the task is to generate a random string of size N. Examples: Input: 5Output: eR3DsInput: 10Output: MPRCyBgdcnUsing a Domain String and Random IndexCreate a domain string that contains small letters, capital letters, and the digits (0 to 9). Then generate a random number pick the character present at that random index and append that character into the answer string. Example : PHP <?php // PHP function to print a // random string of length n function RandomStringGenerator($n) { // Variable which store final string $generated_string = ""; // Create a string with the help of // small letters, capital letters and // digits. $domain = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; // Find the length of created string $len = strlen($domain); // Loop to create random string for ($i = 0; $i < $n; $i++) { // Generate a random index to pick // characters $index = rand(0, $len - 1); // Concatenating the character // in resultant string $generated_string = $generated_string . $domain[$index]; } // Return the random generated string return $generated_string; } // Driver code to test above function $n = 5; echo "Random String of length " . $n . " = " . RandomStringGenerator($n); ?> OutputRandom String of length 5 = EEEto Using random_bytesUse random_bytes in PHP to generate a cryptographically secure random string. Convert random bytes to ASCII values, then map these to characters from a predefined set, ensuring the random string meets the specified length.Example: PHP <?php function generateRandomString($length) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomBytes = random_bytes($length); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[ord($randomBytes[$i]) % $charactersLength]; } return $randomString; } // Example Usage echo generateRandomString(5); // Output: e.g., eR3Ds echo "\n"; echo generateRandomString(10); // Output: e.g., MPRCyBgdcn ?> OutputwWRP4 Wyosn2gNkq Comment More infoAdvertise with us Next Article Program to generate random string in PHP P pk_tautolo Follow Improve Article Tags : Web Technologies PHP PHP-string Similar Reads How to generate a random String in Ruby? In this article, we will learn how to generate a random String in Ruby. Approach to generate a random String in Ruby:When you need to generate a random alphanumeric string of a specified length in Ruby, you have a couple of options. If you are using Ruby version >= 2.5, Ruby, you can simply go wi 2 min read R Program to Generate a Random Password Password generation is a common task in programming languages. It is required for security applications and various accounts managing systems. A random password is not easily guessable which also improves the security of the accounts systems with the aim to protect information. In R Programming Lang 4 min read Program to generate a random two-digit number Write a program to generate a random two-digit number. Example 1: 73Example 2: 26 Approach: To solve the problem, follow the below idea: We can generate a random two-digit number by generating a random integer first and then modulo it by 90. Now, after modulo 90 the remainder can be in the range 0 t 2 min read C# - Randomly Generating Strings In C#, a string is a sequence of Unicode characters or an array of characters. The range of Unicode characters will be U+0000 to U+FFFF. A string is the representation of the text. In this article, we will learn how to randomly generate strings and alphanumeric strings. So to do the task we use the 6 min read How to Generate Random Numbers in R Random number generation is a process of creating a sequence of numbers that don't follow any predictable pattern. They are widely used in simulations, cryptography and statistical modeling. R Programming Language contains various functions to generate random numbers from different distributions lik 2 min read Like