note 106004 modified in function.rand by danbrown

From: Date: Mon, 03 Oct 2011 13:13:37 +0000
Subject: note 106004 modified in function.rand by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-183606@lists.php.net to get a copy of this message
I couldn't find a suitable random alpha-numeric generator function so I rolled my own. It gives a random number in base 36 (0-9, a-z) to a given length. <?php function randomAlphaNum($length){ $rangeMin = pow(36, $length-1); //smallest number to give length digits in base 36 $rangeMax = pow(36, $length)-1; //largest number to give length digits in base 36 $base10Rand = mt_rand($rangeMin, $rangeMax); //get the random number $newRand = base_convert($base10Rand, 10, 36); //convert it return $newRand; //spit it out } ?> hopefully helps someone --was-- I couldn't find a suitable random alpha-numeric generator function so I rolled my own. It gives a random number in base 36 (0-9, a-z) to a given length. function randomAlphaNum($length){ $rangeMin = pow(36, $length-1); //smallest number to give length digits in base 36 $rangeMax = pow(36, $length)-1; //largest number to give length digits in base 36 $base10Rand = mt_rand($rangeMin, $rangeMax); //get the random number $newRand = base_convert($base10Rand, 10, 36); //convert it return $newRand; //spit it out } hopefully helps someone http://php.net/manual/en/function.rand.php

« previous php.notes (#183606) next »