Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Discussion: Random 32 bit string Function

Jim Bunderfeld
The Coder
Join date: 1 Mar 2004
Posts: 161
06-08-2006 13:30
Hey I thought I would just post this to help people learn LSL, most coders probaby know how to do this quite simply.

CODE

//Random 32-bit String Function
// Written by Jim Bunderfeld 6/8/06 MM/DD/YY
// Der Monat/Der Tag/Das Jahr
// Released under GPL License
// Licensing information at http://www.gnu.org/copyleft/gpl.html
// Danke xD
string Random32String()
{
string characters = "abcdefghijklmnopqrstuvwxyz0123456789~`!@#$%^&*()_+-=";
integer count = llStringLength(characters);
integer i;
string result;
for(i = 0;i < 32;i ++)
{
integer rand = (integer)llFrand((float)count - 0);
result += llGetSubString(characters,rand,rand++);
}
return result;
}
default
{
state_entry()
{
llSay(0,Random32String());
}
}
_____________________
| - Linux Users - | #SecondLifeLinux on IRC.EnterTheGame.com
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Original Thread
06-08-2006 21:04
/15/37/112610/1.html
_____________________
i've got nothing. ;)
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
06-09-2006 14:35
Uh, isn't the rand++ unnecessary? It works the same without it, and probably a bit faster.

Also, count doesn't need to be explicity typecast to a float. And what's with the " - 0"?
Sheila Plunkett
On The Prowl!
Join date: 25 Dec 2005
Posts: 67
06-10-2006 02:47
*looks conspiratively*
Sssh! It's there to confuse the *comes closer, whispering into your ear* newbies....
Keiki Lemieux
I make HUDDLES
Join date: 8 Jul 2005
Posts: 1,490
06-10-2006 04:35
If you are going to use a function like this a lot, you would probably want to use a do/while loop since they are faster than for loops.
_____________________
imakehuddles.com/wordpress/
Lee Dimsum
Registered User
Join date: 22 Feb 2006
Posts: 118
06-10-2006 10:17
The function is a bit slow :(
Needs some seconds to generate a 256-bit random string.
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
06-10-2006 11:36
Here, I modified it a bit, and now it has more characters and is faster.

CODE

//Random 32-bit String Function
// Written by Jim Bunderfeld 6/8/06 MM/DD/YY
// Fixed by Keknehv Psaltery 6/10/06
// Der Monat/Der Tag/Das Jahr
// Released under GPL License
// Licensing information at http://www.gnu.org/copyleft/gpl.html
// Danke xD

//Having these variables global will speed up the loop
string characters = "!\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
integer count = 70;
integer i;
string result;
integer rand;
integer nc = 33;
string Random32String()
{
i=0;result="";
while ( ++i < nc )
result += llGetSubString(characters,rand=(integer)llFrand(count),rand);
return result;
}
default
{
state_entry()
{
llSay(0,Random32String());
}
}
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
06-11-2006 04:53
I couldn't help my self.

CODE

//Random 32-bit String Function
// Written by Jim Bunderfeld 6/8/06 MM/DD/YY
// Fixed by Keknehv Psaltery 6/10/06
// Beaten to death by Strife Onizuka 6/11/06
// Veni / Vidi / Vici
// Der Monat/Der Tag/Das Jahr
// Released under GPL License
// Licensing information at http://www.gnu.org/copyleft/gpl.html
// Danke xD

//Having these variables global will speed up the loop
string characters =
"!\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
integer count = 70;
integer i;
string result;
integer rand;
integer nc = 32;
string Random32String()
{
i=nc;result="";
do
result += llGetSubString(characters,rand=(integer)llFrand(count),rand);
while ( --i );
return result;
}
default
{
state_entry()
{
llSay(0,Random32String());
}
}
_____________________
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
Lee Dimsum
Registered User
Join date: 22 Feb 2006
Posts: 118
06-11-2006 07:43
Create a random string with the length x:
CODE

//Random String Function
// Written by Jim Bunderfeld 6/8/06 MM/DD/YY
// Fixed by Keknehv Psaltery 6/10/06
// Beaten to death by Strife Onizuka 6/11/06
// Updated by Lee Dimsum 6/11/06
// Veni / Vidi / Vici
// Der Monat/Der Tag/Das Jahr
// Released under GPL License
// Licensing information at http://www.gnu.org/copyleft/gpl.html
// Danke xD

//Having these variables global will speed up the loop
string characters =
"!\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
integer count = 70;
integer i;
string result;
integer rand;
string RandomString(integer x)
{
i=x;result="";
do
result += llGetSubString(characters,rand=(integer)llFrand(count),rand);
while ( --i );
return result;
}
default
{
state_entry()
{
llSay(0,RandomString(128));
}
}
Jim Bunderfeld
The Coder
Join date: 1 Mar 2004
Posts: 161
06-12-2006 16:02
Yay for GPL :-)
_____________________
| - Linux Users - | #SecondLifeLinux on IRC.EnterTheGame.com
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
06-12-2006 16:53
Heck, let's finish it off by making it the full printable ASCII charset except for spaces. And removing the repetitive i=x, when we can just have x.

CODE

//Random String Function
// Written by Jim Bunderfeld 6/8/06 MM/DD/YY
// Fixed by Keknehv Psaltery 6/10/06
// Beaten to death by Strife Onizuka 6/11/06
// Updated by Lee Dimsum 6/11/06
// Finished by Keknehv Psaltery 6/12/06
//
// Veni / Vidi / Vici
// Der Monat/Der Tag/Das Jahr
// Released under GPL License
// Licensing information at http://www.gnu.org/copyleft/gpl.html
// Danke xD

//Having these variables global will speed up the loop
string characters =
"!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
integer count = 94;
string result;integer rand;
string RandomString(integer x)
{
result="";
do
result += llGetSubString(characters, rand=(integer)llFrand(count) , rand );
while ( --x );
return result;
}
default
{
state_entry()
{
llSay(0,RandomString(128));
}
}


That should be everything, unless someone feels a compelling desire to include a function to change the charset.

Also, how was this originally a random "32 bit" string? 32 Bytes, maybe, although I doubt even that. Have you ever tried making a script that echos what you say, and sent it an ANSI character? It will respond with the same thing, which makes me suspect that each character in a string might actually take two bytes. I'll have to do more research.

And why is this fragment GPL? It seems rather pointless to license it as GPL to me... Why not LGPL or just unrestricted? The function is 12 lines of code, nothing too complex.
Candle Hazlitt
C.D.S.V. Owner
Join date: 6 May 2006
Posts: 25
07-22-2006 19:40
in my experience making the characters into a list instead of a string would make the operation go much faster, because using llGetSubString is very slow, especially with big strings.
_____________________