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 9049599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:26:23+00:00 2026-06-16T12:26:23+00:00

A while back I wrote a random string generator that builds a string using

  • 0

A while back I wrote a random string generator that builds a string using the mt_rand()th character in a string until the desired length is reached.

public function getPassword ()
{
    if ($this -> password == '')
    {
        $pw             = '';
        $charListEnd    = strlen (static::CHARLIST) - 1;
        for ($loops = mt_rand ($this -> min, $this -> max); $loops > 0; $loops--)
        {
            $pw .= substr (static::CHARLIST, mt_rand (0, $charListEnd), 1);
        }
        $this -> password   = $pw;
    }
    return $this -> password;
}

(CHARLIST is a class constant containing a pool of characters for the password. $min and $max are length contraints)

Today, when researching something else entirely I stumbled upon the following code:

function generateRandomString ($length = 10) {    
    return substr(str_shuffle ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}

This accomplishes pretty much the same effect as my looping mt_rand() based code in one line. I really like it for that simple reason, fewer lines of code is always a good thing. 🙂

But when I looked up str_shuffle in PHP’s manual the documentation on it was pretty light. One thing I was really keen to learn was what algorithm does it use for randomness? The manual doesn’t mention what kind of randomization is done to get the shuffled string. If it uses rand() instead of mt_rand() then sticking to my current solution may be better after all.

So basically I’d like to know how str_shuffle randomizes the string. Is it using rand() or mt_rand()? I’m using my random string function to generate passwords, so the quality of the randomness matters.

UPDATE: As has been pointed out, the str_shuffle method is not equivalent to the code I’m already using and will be less random due to the string’s characters remaining the same as the input, only with their order changed. However I’m still curious as to how the str_shuffle function randomizes its input string.

  • 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-06-16T12:26:24+00:00Added an answer on June 16, 2026 at 12:26 pm

    A better solution would be mt_rand which uses Mersenne Twister which much more better.

    As has been pointed out, the str_shuffle method is not equivalent to the code I’m already using and will be less random due to the string’s characters remaining the same as the input, only with their order changed. However I’m still curious as to how the str_shuffle function randomizes its input string.

    To make the output equal lets just use 0,1 and look at the visual representation of each of the functions

    Simple Test Code

    header("Content-type: image/png");
    $im = imagecreatetruecolor(512, 512) or die("Cannot Initialize new GD image stream");
    $white = imagecolorallocate($im, 255, 255, 255);
    for($y = 0; $y < 512; $y ++) {
        for($x = 0; $x < 512; $x ++) {
            if (testMTRand()) { //change each function here 
                imagesetpixel($im, $x, $y, $white);
            }
        }
    }
    imagepng($im);
    imagedestroy($im);
    
    function testMTRand() {
        return mt_rand(0, 1);
    }
    
    function testRand() {
        return rand(0, 1);
    }
    
    function testShuffle() {
        return substr(str_shuffle("01"), 0, 1);
    }
    

    Output testRand()

    enter image description here

    Output testShuffle()

    enter image description here

    Output testMTRand()

    enter image description here

    So basically I’d like to know how str_shuffle randomizes the string. Is it using rand() or mt_rand()? I’m using my random string function to generate passwords, so the quality of the randomness matters.

    You can see clearly that str_shuffle produces almost same output as rand …

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

Sidebar

Related Questions

A while back I was playing with methods using variable-length argument lists (java) that
A while back I created a lightbox plugin using jQuery that would load a
I wrote a scraper using python a while back, and it worked fine in
So here's the scoop: I wrote a tiny C# app a while back that
I while back I wrote a shell script that automatically runs a python script
I wrote a small game a while back in Flash that I'm now re-coding
I opened a js file that I wrote a while back and although it's
I wrote a script a while back that would do some simple install procedures
A while back I wrote a simple python program to brute-force the single solution
I wrote a custom WCF binding a while back to allow my load balanced

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.