Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryTutorials and Answers

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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.

The code is a demonstration of how simple it can be to get a random character or series of characters.

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

Y

Random lower-case character

s

100 random upper-case characters

K O I J I W C T Q N F P S R U R J P M W E W A V T W W C U R W H M J C N X X W U I T J Y T N Q M O L W T X L K Y X K D D L Z Y P M M X I S V W S H B R B P S X Y D U T H X J A Q X P M F Q W U M Y O S K

100 random lower-case characters

u o p f d r d d i h m m w u r x s y g g x c n k u q w o m z t r i j v c s i v h x u o x l m e v q v i d u m n r n q o v v e h u m l h m l o q u e s i n o l b c n b t l z x m n q g s l j o d e x j v v

100 random mixed-case characters

(Random selections from 52 characters.)

X p L T m P x r n I R P k Q U u R N I c O b b P n V s g l C o G D a W C m L m k K T v n K R z q z v N A m S G m B Z S X w V E m q S k U h B J z z s d e e R e v J f h a a h k c B r x X F C O B l Y a y

Reload 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:

O

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:

w

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:

S P T C U I E P Z E A V H C E V U J A A Y N P E G J B I D E O E F C B J B N T G C J K B H E M T S Y H Z M L Z Q H U H W L T G J M O U D A S F M K Q I O L R R O E G B C S N A W C W L X K R N S F J M V

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:

o c v m u m a c o j b g h t d t z x r e c z p t i i d o f q t i d r j g r s p j f n p f q o b v n z f q u d v r y b l l p t b q z b z p b j q o b w q c g c e t u k a s u v s o m r i w u c s i a y b u

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:

l r F t Q E Z G x Q l O R D V t i u D R j F n n M t B e e B e Z X Q w l v R F K Z y z U K g p G g n v N I s g N Z C V o Y X U G p S J a f N M c n g R g K M F o j m G X h T s k W o a v S o t H v k b q

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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC