TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   What do you use for random integers? (http://www.talkphp.com/absolute-beginners/2925-what-do-you-use-random-integers.html)

Enfernikus 06-08-2008 02:36 AM

What do you use for random integers?
 
Well we've all needed to produce random numbers/letters/symbols at one time or another for a salt/temp password or something of the like and I'm curious as to wether y'all use a set function or something else. This is what I use

PHP Code:


function generateValue($len 20){
 
$ret '';
 for(
$i 0$i <= $len;++$i){
   
$ret .= chr(0x3 rand(0,80));
  }
  return 
$ret;


If I wanna go with ONLY digits or letters

[php]

Generally if I want random #'s only I'll just use mktime

delayedinsanity 06-08-2008 02:46 AM

Just off the top of my head if I wanted a quick random number, I'd probably just use

PHP Code:

echo $szString str_shuffle(time());
// or for a little more random
echo $szString str_shuffle(mktime(000mt_rand(112), mt_rand(130), mt_rand(19802010))); 

...but the function I use for random strings looks more like this:

PHP Code:

static function generateRandStr ($intLength$intEncode=0) {

    
// Initialize the string
    
$szRand = (string) "";

    
// Generate a fairly random string
    
for ($i 0$i $intLength$i++) {
        
$intRand mt_rand(0,62);

        
// 0 thru 9
        
if ($intRand 10) {
            
$szRand .= chr($intRand+48);

        
// A thru Z
        
} else if( $intRand 36) {
            
$szRand .= chr($intRand+55);

        
// a thru z
        
} else {
            
$szRand .= chr($intRand+61);
        }

    }
  
    
// Determine if the string should be hashed
    
if ($intEncode == 1) return md5($szRand);
    if (
$intEncode == 2) return sha1($szRand);
        return 
$szRand;




Evulness 06-09-2008 01:41 PM

i've been using this, its been working fine for helping to create my captcha strings.

Code:

public function EvRandString($length=10, $chrs = '1234567890qwertyuiopasdfghjklzxcvbnm'){
for($i=0; $i<$length; $i++){
    $result .= $chrs{mt_rand(0, strlen($chrs)-1)};
}
return $result;
}


Aaron 06-09-2008 04:43 PM

Lemme try to come up with something...

PHP Code:

$length "5";

// Different levels of security
$batch1  'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'// Full Alpha
$batch2 .= '1234567890'// Numerics
$batch3 .= '!@#$%^&*()_+90-=[]\{}|;:,./<>?';  // Simple symbols, no quotation marks
$batch4 .= '§†↓■«╓≡áíóú█æ¥Ð╗ö╫⌠╞└♦â'// Ghasp...

// Process
$secureNum mtrand(1,4);

for (
$int=0$int<$length$int++){
  switch 
$secureNum
    
case 1:
    
$batch1...
... 
I forgot >.<


If anyone could finish that for me that would be sweet, because I totally forgot it.

SpYkE112 06-09-2008 04:50 PM

I just made this one earlier today:
PHP Code:

function randStr($len 32) {
        
$str "";
        
        for(
$i 0$i $len$i++)
        {
            
$str .= chr(rand(33,126));
        }
        
        return(
$str);
    } 

Which is, full alphabet (upper and lower case), some funny char's and numbers :)

Aaron 06-09-2008 04:52 PM

I must really suck at this... You know what!?
PHP Code:

$pie mkrand(0,bazillion);

$pie md5($pie);
echo 
"I win!"


xenon 06-09-2008 08:46 PM

Not really JUST a random integer generator, but a common one :-)

PHP Code:

$hash_table array_merge(range('a''z'), range('A''Z'), range(09));
$code_length 6;
$code '';

for(
$i 0$i $code_length$i++)
{
    
shuffle($hash_table);

    
$code .= $hash_table[rand(0count($hash_table) - 1];
}

echo 
$code

8-)


All times are GMT. The time now is 01:59 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0