Solved

create random string

Posted on 2011-09-23
1
306 Views
Last Modified: 2012-05-12
Hello, I have a function which is supposed to create a random 8 character string, it works most of the time, but sometimes it creates a 7 character string.. very weird, please help...

function genRandomString() {
    $length = 8;
    $characters = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
    $string = "";    
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;
}
0
Comment
Question by:jblayney
1 Comment
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 36588071
Try to fix your function this way:


function genRandomString() {
    $length = 8;
    $characters = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
    $string = "";    
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters)-1)];
    }
    return $string;
}

Cheers
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php convert date format 3 22
PHP and google maps 13 45
PHP Installer 5 25
How can I implement a "Select All" with this configuration...? 6 42
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question