php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #9758 Is there any way that you can create a random letter from A-Z?
Submitted: 2001-03-14 17:41 UTC Modified: 2001-03-14 17:55 UTC
From: brad at simplysmarter dot org Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.0.4pl1 OS: Unix
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: brad at simplysmarter dot org
New email:
PHP Version: OS:

 

 [2001-03-14 17:41 UTC] brad at simplysmarter dot org
I would like to be able to create a random letter from A-Z for a member login authorization code.  I would like to have a 7 digit number ie: A543BC21 so that they can create a username and password.  I know how to randomize a number (ie: mt_rand( 0, 9 );) but how can I randomize letters?  I tried using mt_rand( "A", "Z" ) but as I expected, it gave me an "Invalid Range" error.  I could do this:
<?
$letter = mt_rand( 1, 36 );
if( $letter == "1" )
     {
     $letter = "A";
     }
//etc. like this for A-Z
?>
but it would be exceptionally messy and way too long.  Is there any other way to do it?  If it doesn't exist, can you add this to the next version of PHP?

Thanks so much!

Brad Taylor
Senior Web Developer
BradCom Industries LTD
www.simplysmarter.org

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-03-14 17:55 UTC] torben@php.net
This isn't the place for questions. Something like the following
should work. I haven't tested it.

<?php
$ind = mt_rand(0, 25);
$alphabet = 'abcdefghijklmnopqrstuvwxyz';
$my_random_char = $alphabet[$ind];
?>

In the future, please use the mailing lists for coding questions.
http://www.php.net/support.php

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 11:01:27 2024 UTC