Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   randomstring2 (https://forums.graalonline.com/forums/showthread.php?t=83422)

LoneAngelIbesu 12-30-2008 12:56 AM

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(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;


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.source10true));
  echo(
randomstring2(temp.source10false));


Outputs:
Quote:

wcuA7vw,Cr
zeC@-K;QP)

Tigairius 12-30-2008 02:01 AM

Seems like it would be easier & a little safer to do something like:
PHP Code:

temp.source "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz013456789!@#$^&*()_+[]/,?.|;:";
temp.limit 25;
temp.repeat false;
temp.output "";

for (
temp.0temp.limittemp.++) {
  
temp.randomout source.charat(int(random(0source.length())));
  if (!
repeat && output.pos(randomout) >= 0) {
    if (
limit <= source.length()) {
      
temp.--;
      continue;
    }else
      break;
  }
  
temp.output @= randomout;
}

return 
output

Otherwise it's nice :)

LoneAngelIbesu 12-30-2008 02:39 AM

Quote:

Originally Posted by Tigairius (Post 1453355)
Seems like it would be easier & a little safer to do something like:

What's the upside to using a string, rather than an array? I imagine that an array offers the possibility of using multiple-character bits, instead of just single character bits.

Tigairius 12-30-2008 02:53 AM

Quote:

Originally Posted by LoneAngelIbesu (Post 1453357)
What's the upside to using a string, rather than an array? I imagine that an array offers the possibility of using multiple-character bits, instead of just single character bits.

No upside to using a string, I just didn't feel like doing temp.source = {"A", "B", "C", etc}; >_<
I think it's generally bad practice to use a while() loop where a for() loop can be used, and right now it doesn't look like you have any loop protection, so if you input incorrect parameters you can break the loop limit.

xXziroXx 12-30-2008 12:14 PM

Quote:

Originally Posted by Tigairius (Post 1453359)
No upside to using a string, I just didn't feel like doing temp.source = {"A", "B", "C", etc}; >_<
I think it's generally bad practice to use a while() loop where a for() loop can be used, and right now it doesn't look like you have any loop protection, so if you input incorrect parameters you can break the loop limit.

Well... His custom title do say master of infinite loops. :rolleyes:

LoneAngelIbesu 12-31-2008 03:35 AM

Quote:

Originally Posted by Tigairius (Post 1453359)
No upside to using a string, I just didn't feel like doing temp.source = {"A", "B", "C", etc}; >_<
I think it's generally bad practice to use a while() loop where a for() loop can be used, and right now it doesn't look like you have any loop protection, so if you input incorrect parameters you can break the loop limit.

I did use a for() loop, originally. However, that results in temp.i increasing, even though nothing is added to temp.output. That results in outputs that aren't the length defined by temp.limit.

Also, I have no clue what "loop protection" is. A search leads only to this topic.

Tigairius 12-31-2008 04:46 AM

Quote:

Originally Posted by LoneAngelIbesu (Post 1453568)
Also, I have no clue what "loop protection" is. A search leads only to this topic.

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.

Chompy 12-31-2008 04:56 AM

Quote:

Originally Posted by Tigairius (Post 1453577)
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.

If repeat is true then it will do that. Easy fix would be to return when output size equals source size

Tigairius 12-31-2008 06:28 AM

Quote:

Originally Posted by Chompy (Post 1453579)
If repeat is true

Yes, that's what I meant ^^

Pelikano 12-31-2008 03:47 PM

There is no reason to make a password longer than 8 chars, is there?

[email protected] 12-31-2008 04:17 PM

not only for passes i suppose.
however, for password purposes I would prefer if the player was to write a password to remember, nothing generated...

Crow 12-31-2008 04:58 PM

Quote:

Originally Posted by [email protected] (Post 1453642)
not only for passes i suppose.
however, for password purposes I would prefer if the player was to write a password to remember, nothing generated...

I prefer generated. If I have to choose a password myself, I always use one of my generated ones. Since most of them use lowercase letters, uppercase letters and numbers, and some even use additional special symbols, it takes longer to bruteforce them :p

Pelikano 12-31-2008 05:40 PM

bruteforcing a 8 letter password takes about 2-3 years

DustyPorViva 12-31-2008 06:52 PM

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.

Crow 12-31-2008 06:56 PM

Quote:

Originally Posted by Pelikano (Post 1453652)
bruteforcing a 8 letter password takes about 2-3 years

Please inform yourself about things you don't know about before posting anything.

Pelikano 12-31-2008 08:54 PM

xD

LoneAngelIbesu 12-31-2008 10:05 PM

Quote:

Originally Posted by Tigairius (Post 1453577)
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 (Post 1453671)
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


All times are GMT +2. The time now is 05:06 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.