Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

random string generator

Cherry Paisley
Registered User
Join date: 27 Oct 2006
Posts: 1
12-30-2006 20:12
Hello,

Can someone help me with a function that will generate a random string / password?

Any help will be much much appreciated!

Thanks,
Cherry
Edison Swain
Registered User
Join date: 7 Dec 2006
Posts: 51
12-30-2006 22:44
Hi Cherry,

This function will generate a random password of letters and numbers for you - I hope this is what you are looking for:

CODE

string fGenerateRandomPassword(integer vMinPasswordLength, integer vMaxPasswordLength)
{
list vPasswordCharacters = ["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"];
integer vPasswordLength = vMinPasswordLength + llFloor(llFrand(vMaxPasswordLength - vMinPasswordLength + 1));
integer vListLength = llGetListLength(vPasswordCharacters);
string vPassword = "";
integer vCounter;

vPassword += llList2String(vPasswordCharacters, llFloor(llFrand(26)));

for (vCounter = 2; vCounter <= vPasswordLength; vCounter++)
{
vPassword += llList2String(vPasswordCharacters, llFloor(llFrand(vListLength)));
}

return vPassword;
}


Some details:

- this will generate a random password that has a length anywhere between the two parameters passed into it.

- each password begins with an alphabetic character, followed by any alphanumeric characters

- it's not case sensitive: it only spits out lowercase letters

- it should be easily modifiable if you want to include uppercase letters or other characters (like underscores) just add them to the list "vPasswordCharacters"

- it uses llFrand, so it's probably "pseudo-random" as opposed to purely random

- this includes no error error checking: try passing negative parameters and you probably won't get results that you are happy with! just keep the vMaxPasswordLength parameter larger than the vMinPasswordLength parameter, and everything should work fine
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
12-31-2006 02:39
Switch to using a straight string and llGetSubString rather than a list for the encryption characters would be more efficient in terms of memory usage.
Edison Swain
Registered User
Join date: 7 Dec 2006
Posts: 51
12-31-2006 09:42
From: Newgate Ludd
Switch to using a straight string and llGetSubString rather than a list for the encryption characters would be more efficient in terms of memory usage.


Yeah - I figured as much. I forgot to mention that my method was probably not the most efficient, but forgot to add that in! (I'm still quite new to scripting and was just happy to be able to do it!)
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
12-31-2006 09:47
From: Edison Swain
Yeah - I figured as much. I forgot to mention that my method was probably not the most efficient, but forgot to add that in! (I'm still quite new to scripting and was just happy to be able to do it!)


You solution is reasonable just not how I would do it :).
You could also have used llListRandomize rather than select the letters teh way you did.
Edison Swain
Registered User
Join date: 7 Dec 2006
Posts: 51
12-31-2006 10:22
From: Newgate Ludd
You solution is reasonable just not how I would do it :).
You could also have used llListRandomize rather than select the letters teh way you did.


That was my first thought, but I believe that won't allow for repeating characters. For example, you'd never end up with "eg7tt7g" or something like that. Not that it probably matters though, since forcing it to be, say, 8 characters out of the 36 possible will still give around a trillion different possibilities without repeating characters.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
12-31-2006 14:22
CODE

string fGenerateRandomPassword(integer vMinPasswordLength, integer vMaxPasswordLength)
{//remove the " + " forum software needs spaces every 64 char or it inserts them
string vPasswordCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJ" + "KLMNOPQRSTUVWXYZ0123456789";
integer vPasswordLength = vMinPasswordLength + (integer)llFrand(-~vMaxPasswordLength - vMinPasswordLength);
integer pos = (integer)llFrand(52);
string vPassword = "";

if (vPasswordLength > 0)//idiot check
do{
vPassword += llGetSubString(vPasswordCharacters, pos,pos);
do;while((pos = (integer)llFrand(62)) == pos);//no sequential repeats.
}while(vPasswordLength = ~-vPasswordLength);

return vPassword;
}
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
grumble Loudon
A Little bit a lion
Join date: 30 Nov 2005
Posts: 612
01-01-2007 12:11
See the wiki page on Frand

It is not random and will give you the same sequence after a reset.
Sterling Whitcroft
Registered User
Join date: 2 Jul 2006
Posts: 678
01-01-2007 19:50
Can you add llGetTimestamp to Frand to generate an (approximate) random seeded number?
or at least one that doesn't repeat?
Peekay Semyorka
Registered User
Join date: 18 Nov 2006
Posts: 337
01-01-2007 21:51
From: grumble Loudon
It is not random and will give you the same sequence after a reset.


No, llFrand will not give the same sequence after a script reset, so in effect it is internally seeded.

-peekay
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
01-02-2007 13:44
From: grumble Loudon
See the wiki page on Frand

It is not random and will give you the same sequence after a reset.


That information was factually inaccurate and was removed from the wiki. Please read the revised page:
http://lslwiki.com/lslwiki/wakka.php?wakka=llFrand

Test script to illustrate:
CODE

default
{
state_entry()
{
integer a = 1 + (integer)llGetObjectDesc();
llSetObjectDesc((string)a);
integer b = 100;
list c;
do
c += (integer)llFrand(10.0);
while(--b);
llOwnerSay(llList2CSV(c));
if(a < 2)
llResetScript();
}
}
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey