PHP Math Exercises: Generate random 11 characters string of letters and numbers
Write a PHP script to generate random 11 characters string of letters and numbers.
Visual Presentation:
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:
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP script which rounds the specified values with 1 decimal digit precision.
Next: Write a PHP script to convert scientific notation to an int and a float.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics