Software, your way.
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

R

Random lower-case character

t

100 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 U

100 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 c

100 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 B

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:

N

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:

s

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:

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

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:

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

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:

Y A k m N a B v E P Z Z l n c A N J B c e d Y c n W L v E w g w B z w m C F Y W I Q v z n e B y t E n g W D b A v b g q e j a l H o Y p o B G a s E S z f f a P R t G r p n O g i g A S Y h b p A I n m

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