Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7090049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:59:49+00:00 2026-05-28T07:59:49+00:00

For generating random string i written code like this But if the function is

  • 0

For generating random string i written code like this But if the function is called with a negative value, or zero, or 1 million it will create lot of problems. So please suggest me better code.

/*
* The following function is for generating a random string value.
* @param unknown_type $length
* @return string $randString
*/
function generateRandomString( $length ) {
    $randString = "";
    srand();
    $characters = "abcdefghijklmnopqrstuvwxyz1234567890";
    /*
    *   build string of random characters
    */ 
    while( strlen( $randString ) < $length ) {
        $randString .= substr( $characters, rand() % (strlen( $characters )),1 );
    }
return $randString;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:59:50+00:00Added an answer on May 28, 2026 at 7:59 am

    To stop the negative number or zero passed as an argument as being a problem, you could use max(1, $length). Alternatively, check for < 1 and throw an exception or return FALSE.

    So please suggest me better code.

    Here are some suggestions for your code…

    function generateRandomString( $length ) {
        $chars = array_merge(range('a', 'z'), range(0, 9));
        shuffle($chars);
        return implode(array_slice($chars, 0, $length));
    }
    

    CodePad.

    Note that this won’t ever have the same character twice in the resulting string.

    If you do desire that, you could use something like…

    function generateRandomString( $length ) {
        $chars = array_merge(range('a', 'z'), range(0, 9));
    
        return array_reduce(array_rand($chars, $length), function($str, $key) use ($chars) { 
            return $str.= $chars[$key];
        }, ''); 
    
    }
    

    CodePad.

    …or slightly different…

    function generateRandomString( $length ) {
        $chars = array_merge(range('a', 'z'), range(0, 9));
    
        return implode(array_map(function($key) use ($chars) { 
            return $chars[$key];
        }, array_rand($chars, $length)));   
    
    }
    

    CodePad.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently generating an 8-character pseudo-random uppercase string for A .. Z: value =
i am generating random password. but i want to get phonetic string of the
This is a WinForm written in C#. Lets say I'm generating a random named
I am in need of generating a random string with spaces and mixedCase. This
I'm looking for tools for generating random but realistic text. I've implemented a Markov
I’m generating multiple, random sized, circular elements using the Raphael JavaScript library but because
I am trying to code a random number generation function in embedded C where
I've build a random string generator but I'm having a problem whereby if I
I am considering this random string generator in perl: sub generate_random_string { my $length
Generating a random password is easy. but generating a batch is more difficult. public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.