TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-08-2008, 02:36 AM   #1 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default 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
Enfernikus is offline  
Reply With Quote
Old 06-08-2008, 02:46 AM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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;


delayedinsanity is offline  
Reply With Quote
Old 06-09-2008, 01:41 PM   #3 (permalink)
The Contributor
 
Evulness's Avatar
 
Join Date: Apr 2008
Location: Tampa, FL
Posts: 65
Thanks: 6
Evulness is on a distinguished road
Default

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;
}
__________________
"Knowledge is power. Abuse it."~Evulness
My portfolio: www.evularts.com
Send a message via AIM to Evulness
Evulness is offline  
Reply With Quote
Old 06-09-2008, 04:43 PM   #4 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

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.
__________________
Signatures are nothing but incriminating.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 06-09-2008, 04:50 PM   #5 (permalink)
The Contributor
 
Join Date: May 2008
Location: Denmark
Posts: 70
Thanks: 3
SpYkE112 is on a distinguished road
Default

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 :)
SpYkE112 is offline  
Reply With Quote
Old 06-09-2008, 04:52 PM   #6 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

I must really suck at this... You know what!?
PHP Code:
$pie mkrand(0,bazillion);

$pie md5($pie);
echo 
"I win!"
__________________
Signatures are nothing but incriminating.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 06-09-2008, 08:46 PM   #7 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

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
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.

Last edited by xenon : 06-10-2008 at 09:32 AM.
xenon is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:22 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design