NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page [TUTORIAL]Generating Random string

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search
7 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 08-27-2009, 07:13 AM THREAD STARTER               #1 (permalink)
NamePros Member
Join Date: Aug 2009
Posts: 26
manqka is an unknown quantity at this point
 



[TUTORIAL]Generating Random string


Just simple code to generate random strings:

You can change the chars in $chars

PHP Code:
<?
function RandomString($length)
{
    
$chars "abcdefghijklmnopqrstuvwxyz0123456789";
????: NamePros.com http://www.namepros.com/code/606140-tutorial-generating-random-string.html
    
srand((double)microtime() * 1000000);
    
$str "";
????: NamePros.com http://www.namepros.com/showthread.php?t=606140
    
$i 0;
    
        while (
$i $length) {
            
$num rand() % 33;
            
$tmp substr($chars$num1);
            
$str $str $tmp;
            
$i++;
        }
    return 
$str;
}

$string RandomString(5);

echo 
"$string";
?>
manqka is offline   Reply With Quote
Old 08-27-2009, 09:02 AM   #2 (permalink)
NamePros Regular
 
chadsmith's Avatar
Join Date: Jul 2008
Location: Wichita, KS
Posts: 287
chadsmith is just really nicechadsmith is just really nicechadsmith is just really nicechadsmith is just really nice
 




Great function. The one I usually use for PHP is:

PHP Code:
<?php
function random_id(){
    
$charset='0123456789abcdefghijklmnopqrstuvqxyzABCDEFGHIJKLMNOPQRSTUVQXYZ';
    
$max=strlen($charset)-1;
    
$id='';
    
$len=32;
    while(
$len--)
        
$id.=$charset[mt_rand(0,$max)];
    return 
$id;
????: NamePros.com http://www.namepros.com/showthread.php?t=606140
}
?>
It appears to be slightly faster. And for JavaScript:

Code:
function random_id(){
	var
		charset='0123456789abcdefghijklmnopqrstuvqxyzABCDEFGHIJKLMNOPQRSTUVQXYZ',
		max=charset.length,
		id='',
		len=32,
		ptr;
	while(len--)
		id+=charset.substring(ptr=Math.floor(Math.random()*max),ptr+1);
	return id
};
chadsmith is offline   Reply With Quote
Old 08-28-2009, 01:35 AM THREAD STARTER               #3 (permalink)
NamePros Member
Join Date: Aug 2009
Posts: 26
manqka is an unknown quantity at this point
 



Yes this is a way too .
manqka is offline   Reply With Quote
Old 08-28-2009, 02:23 AM   #4 (permalink)
Account Suspended
Join Date: Aug 2009
Posts: 164
bodil4o is on a distinguished road
 



Cool tutorial!
Thanks!
bodil4o is offline   Reply With Quote
Reply


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


 
All times are GMT -7. The time now is 07:28 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger