Coding for Random Characters; A Tutorial
This is a tutorial for PHP coders with simple examples for getting random characters.
As you write software, you'll no doubt encounter a need for random-selected characters. It might be for a temporary file name, a security code, or for something else.
The article is code heavy. It is a tutorial, after all.
I believe the code examples are some of the simplest methods that use easily readable code.
The examples all use the echo() function to print the randomly-selected characters to the screen. For your software you would, of course, assign the characters to a variable so it can be used how you need to use it.
Near the end of this article is a PHP script with example code embedded.
Here is an example of the PHP script's output. Examples with code for each of the sections are further below.
Random Character Examples
Random upper-case character
RRandom lower-case character
t100 random upper-case characters
R E H R P V Z D F U W W A I F V B T E F R T E Z F C W K G X K T T E A J J G T F T C I C Z G G B V G L W V O L Z A I G Y I U K F R S T Y J N H W B T X O Q N K C N K U N H E Q S G H K V Y I Z A I S U U100 random lower-case characters
x p p x r s h k i z k r g v b q j j j r x v d j l v f s j c v k s e v r p x v h p f v a k l a m n i d l z k z r m n t w n e r h j k s m e v t i t t z p s s p f u w k q s r v z q b s a a z c t l g f c100 random mixed-case characters
(Random selections from 52 characters.)
H c l H m c l D E G s j S T Y b v t P g L z C V S s E J X L h V Y b q d Z v u F J Z m r H e h R N k X w s N A M C D L O T M p I g B C i K T E p x Y m z X v w h R J q I s m w q w T K q r G l O l Z B BReload this page to get different random characters in the above example output.
Random upper-case character
This code prints one upper-case character. Upper-case characters are in the ASCII table at positions 65 through 90, inclusive.
echo chr( mt_rand(65,90) );
The chr() function accepts a number and returns the ASCII character corresponding to the number.
The mt_rand() function is a fast random number generator. But it's not cryptographically secure. If you need a cryptographically secure random number generator, consider using the slightly slower random_int() function.
Example of the above code in use:
This code assigns a random upper-case character to a variable instead of printing it.
$variable = chr( mt_rand(65,90) );
Random lower-case character
This code prints one lower-case character. Lower-case characters are in the ASCII table at positions 97 through 122, inclusive.
echo chr( mt_rand(97,122) );
Example of the above code in use:
This assigns a random lower-case character to a variable.
$variable = chr( mt_rand(97,122) );
100 random upper-case characters
To print 100 random upper-case characters, the character selection is put into a loop. The code prints a space after each randomly-selected character so the characters can wrap into multiple lines if needed.
for( $i=0; $i<100; $i++ ) { echo chr( mt_rand(65,90) ).' '; }
Example of the above code in use:
This assigns 100 random upper-case characters to a variable. No space is assigned to the variable; all randomly-selected characters are next to each other in a string.
for( $i=0; $i<100; $i++ ) { $variable .= chr( mt_rand(65,90) ); }
100 random lower-case characters
As in the previous section, to print 100 random lower-case characters, the character selection is put into a loop.
for( $i=0; $i<100; $i++ ) { echo chr( mt_rand(97,122) ).' '; }
Example of the above code in use:
This assigns 100 random lower-case characters to a variable as a string of characters.
for( $i=0; $i<100; $i++ ) { $variable .= chr( mt_rand(97,122) ); }
100 random mixed-case characters
The previous examples selected letters from the ASCII table. Because the upper-case and lower-case letters aren't contiguous in the ASCII table, a different technique can be used to randomly select one character from a selection composed of both cases.
The $Characters
variable contains a string of characters for the randomizer to select from. The $numchars
variable contains the number of characters in the strings.
$Characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $numchars = strlen($Characters);
This code prints 100 characters randomly-selected from the string.
for( $i=0; $i<100; $i++ ) { echo substr( $Characters, chr( mt_rand(0,($numchars-1)), 1 ).' '; }
Example of the above code in use:
This assigns 100 random mixed-case characters to a variable as a string of characters.
for( $i=0; $i<100; $i++ ) { $variable .= substr( $Characters, chr( mt_rand(0,($numchars-1)), 1 ); }
The Random Character Examples PHP Software
Copy this code (no customization required) and paste it into a text processor that can save text with UTF-8 character set.
Save the file as randomexamples.php or other .php file name that you prefer.
<?php /* Random Character Examples Version 1.0 June 5, 2017 Will Bontrager Software LLC */ echo '<h1>Random Character Examples</h1>'; /* Selections from ASCII table. */ echo '<h3>Random upper-case character</h3>'; echo chr( mt_rand(65,90) ); echo '<h3>Random lower-case character</h3>'; echo chr( mt_rand(97,122) ); echo '<h3>100 random upper-case characters</h3>'; for( $i=0; $i<100; $i++ ) { echo chr( mt_rand(65,90) ).' '; } echo '<h3>100 random lower-case characters</h3>'; for( $i=0; $i<100; $i++ ) { echo chr( mt_rand(97,122) ).' '; } /* Random character selections from defined string. */ echo '<h3 style="margin-bottom:0;">100 random mixed-case characters</h3>'; $Characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $numchars = strlen($Characters); echo "<p style='margin-top:0;'>(Random selections from $numchars characters.)</p>"; for( $i=0; $i<100; $i++ ) { echo substr( $Characters, mt_rand(0,($numchars-1)), 1 ).' '; } ?>
As you'll see when you look at the source code, the software is a set of examples to demonstrate various ways to get a random character or set of characters. It's intended to be a tutorial.
(This article first appeared in Possibilities newsletter.)
Will Bontrager