Thread: randomstring2
View Single Post
  #1  
Old 12-30-2008, 12:56 AM
LoneAngelIbesu LoneAngelIbesu is offline
master of infinite loops
LoneAngelIbesu's Avatar
Join Date: May 2007
Location: Toldeo, Ohio
Posts: 1,049
LoneAngelIbesu has a spectacular aura aboutLoneAngelIbesu has a spectacular aura about
Send a message via AIM to LoneAngelIbesu
randomstring2

randomstring2(source, limit, repeat); - returns a random string with the length of limit, derived from character in the source array, repeating characters if set to true.
- source: an array of single characters
- limit: a positive number, indicating the size of random string output
- repeat: boolean, indicating whether or not to repeat characters
PHP Code:
function randomstring2(source, limit, repeat) {
  if(
temp.limit == null) {
    
temp.limit = temp.source.size();
  }
  if(
temp.limit > temp.source.size()) {
    
temp.limit = temp.source.size();
  }
  while(
temp.i < temp.limit) {
    
temp.trial = temp.source[random(0, temp.source.size())];
    if(
temp.repeat) {
      
temp.output @= temp.trial;
      
temp.i++;
    }
    else {
      if(
temp.output.pos(temp.trial) < 0) {
        
temp.output @= temp.trial;
        
temp.i++;
      }
    }
  }
  return 
temp.output;
} 
All possible source characters:
PHP Code:
  temp.source = {
    
"A", "B", "C", "D", "E", "F", "G", "H",
    
"I", "J", "K", "L", "M", "N", "O", "P",
    
"Q", "R", "S", "T", "U", "V", "W", "X",
    
"Y", "Z", "a", "b", "c", "d", "e", "f",
    
"g", "h", "i", "j", "k", "l", "m", "n",
    
"o", "p", "q", "r", "s", "t", "u", "v",
    
"w", "x", "y", "z", "0", "1", "2", "3",
    
"4", "5", "6", "7", "8", "9", "!", "@",
    
"#", "$", "%", "^", "&", "*", "(", ")",
    
"[", "]", ";", ":", "'", "<", ">", ".",
    
",", "?", "/", "-", "_", "=", "+", "|"
  
}; 
Example:
PHP Code:
function onCreated() {
  
temp.source = {
    
"A", "B", "C", "D", "E", "F", "G", "H",
    
"I", "J", "K", "L", "M", "N", "O", "P",
    
"Q", "R", "S", "T", "U", "V", "W", "X",
    
"Y", "Z", "a", "b", "c", "d", "e", "f",
    
"g", "h", "i", "j", "k", "l", "m", "n",
    
"o", "p", "q", "r", "s", "t", "u", "v",
    
"w", "x", "y", "z", "0", "1", "2", "3",
    
"4", "5", "6", "7", "8", "9", "!", "@",
    
"#", "$", "%", "^", "&", "*", "(", ")",
    
"[", "]", ";", ":", "'", "<", ">", ".",
    
",", "?", "/", "-", "_", "=", "+", "|"
  
};
  echo(
randomstring2(temp.source, 10, true));
  echo(
randomstring2(temp.source, 10, false));
} 
Outputs:
Quote:
wcuA7vw,Cr
zeC@-K;QP)
__________________
"We are all in the gutter, but some of us are looking at the stars."
— Oscar Wilde, Lady Windermere's Fan

Last edited by Tolnaftate2004; 01-07-2009 at 06:13 PM.. Reason: by request
Reply With Quote