w3resource

PHP Math Exercises: Generate random 11 characters string of letters and numbers


3. Generate Random 11-Character String

Write a PHP script to generate random 11 characters string of letters and numbers.

Visual Presentation:

PHP Math Exercises: Generate random 11 characters string of letters and numbers

Sample Solution:

PHP Code:

<?php
$x = rand(10e12, 10e16); // Generate a random number between 10^12 and 10^16
echo base_convert($x, 10, 36)."\n"; // Convert the random number to base-36 and echo it
?>

Output:

3b6jfwh18v2

Explanation:

In the exercise above,

  • $x = rand(10e12, 10e16);: This line generates a random number between 10^12 and 10^16 using the "rand()" function. The range of the random number is specified using scientific notation, where '10e12' represents 10^12 and '10e16' represents 10^16.
  • echo base_convert($x, 10, 36)."\n";: This line converts the generated random number '$x' from base-10 (decimal) to base-36 using the "base_convert()" function. Base-36 uses the digits 0-9 and letters A-Z to represent numbers. The converted number is then echoed to the output, followed by a newline character ("\n").

Flowchart:

Flowchart: Generate random 11 characters string of letters and numbers

For more Practice: Solve these Related Problems:

  • Write a PHP function that generates an 11-character random alphanumeric string using shuffle() and substr(), ensuring no use of rand().
  • Write a PHP script to generate multiple random strings of 11 characters and verify that each string contains at least one letter and one number.
  • Write a PHP program to create a random 11-character string and then check its uniqueness across successive script executions.
  • Write a PHP script to generate an 11-character string from a custom character pool and validate that it meets a predetermined entropy threshold.

Go to:


PREV : Round Values with 1 Decimal Precision.
NEXT : Convert Scientific Notation to Int and Float.

PHP Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.