Random Password Generator: Difference between revisions

From Second Life Wiki
Jump to navigation Jump to search
characters variable incorrect on several lines
An earlier editor's understanding of string length and base-0 indexes, in conjunction with llFrand was faulty. llFloor() is not needed.
Line 6: Line 6:
string randomPassword(integer length)
string randomPassword(integer length)
{
{
     string characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
     string CharSet = "abcdefghijkmnpqrstuvwxyz23456789";   // omitting confusable characters
     string passwordToBeReturned;
     string password;
    integer CharSetLen = llStringLength(CharSet);
    // Note: We do NOT add 1 to the length, because the range we want from llFrand() is 0 to length-1 inclusive


     while(llStringLength(passwordToBeReturned) < length)
     while (length--)
     {
     {    
        // because llFrand is [0, mag)
         integer rand = (integer) llFrand(CharSetLen);
         integer upperLimit = llStringLength(characters) + 1;
         password += llGetSubString(CharSet, rand, rand);
        integer rand = llFloor(llFrand(upperLimit));
 
         passwordToBeReturned += llGetSubString(characters, rand, rand);
     }
     }
 
     return password;
     return passwordToBeReturned;
}
}
   
   
default
default
{
{
     state_entry()
     touch_start(integer num)
     {
     {
        // PUBLIC_CHANNEL has the integer value 0
         llSay(0, randomPassword(10));
         llSay(PUBLIC_CHANNEL, randomPassword(10));
     }
     }
}
}
</lsl>
</lsl>

Revision as of 11:22, 26 March 2014

Code: <lsl> // Generate Passwords based on String length // Free to use, share and remix.

string randomPassword(integer length) {

   string CharSet = "abcdefghijkmnpqrstuvwxyz23456789";    // omitting confusable characters
   string password;
   integer CharSetLen = llStringLength(CharSet);
   // Note: We do NOT add 1 to the length, because the range we want from llFrand() is 0 to length-1 inclusive
   while (length--)
   {     
       integer rand = (integer) llFrand(CharSetLen);
       password += llGetSubString(CharSet, rand, rand);
   }
   return password;

}

default {

   touch_start(integer num)
   {
       llSay(0, randomPassword(10));
   }

} </lsl>