Thread: randomstring2
View Single Post
  #5  
Old 12-31-2008, 10:05 PM
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
Quote:
Originally Posted by Tigairius View Post
Right now your while loop will go in to an infinite loop if your input limit is greater than the array size of the source.
Oh, I see. Perhaps something like this?
PHP Code:
function randomstring2(sourcelimitrepeat) {
  if(
temp.limit == null) {
    
temp.limit temp.source.size();
  }
  if(
temp.limit temp.source.size()) {
    
temp.limit temp.source.size();
  }
  while(
temp.temp.limit) {
    
temp.trial temp.source[random(0temp.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;

Quote:
Originally Posted by DustyPorViva View Post
I agree that arrays are terrible for this sort of thing, simply because of the time it takes to set them up. I much prefer strings.
Well, I provided a source array that contains all possible single characters. So, it shouldn't take too much time to edit it, if you're simply looking to generate something from a list of single characters. Arrays offer the possibility of doing something like this:
PHP Code:
function onCreated() {
  
temp.source = {
    
"test",
    
"sources",
    
"foo bar",
    
"foo bar baz",
    
"foo!bar!baz"
  
};
  echo(
randomstring2(temp.source3true));
  echo(
randomstring2(temp.source3false));

Which outputs:
Quote:
foo!bar!bazfoo bar bazsources
sourcestestfoo bar baz
__________________
"We are all in the gutter, but some of us are looking at the stars."
— Oscar Wilde, Lady Windermere's Fan
Reply With Quote