Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Script to generate random string

MadamG Zagato
means business
Join date: 17 Sep 2005
Posts: 1,402
01-17-2006 18:10
Anyone have a script to generate a random string that is X amount to characters long? Of course the idea being to be able to specify the number of characters generated.

:)
DoteDote Edison
Thinks Too Much
Join date: 6 Jun 2004
Posts: 790
01-17-2006 19:14
I made a random key generator to randomly search the database for textures. So far, after about 400 keys, none have produced an actual texture (other than "Missing Image";). Anyway, the basic concept follows:

Create a list of characters, ["a", "b", "c", etc...];
Create a function that appends a random string from the list to a string variable.
Loop the function as often as needed, adding to the string each new character.

CODE
list chars = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]; //surely a better way than this
string output = "";

integer length = 1;

default {
integer x;
for(x=0;x<length;x++) {
float y = llFrand(llGetListLength(chars)) - 0.1;
output = output + llList2String(chars, (integer)y);
}
llSay(0, output);
}
Ben Bacon
Registered User
Join date: 14 Jul 2005
Posts: 809
01-18-2006 00:45
From: DoteDote Edison
//surely a better way than this
one way that *might* be better would be to use a string rather than a list.

string chars = "abcdefghi";

anyone know?
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
01-18-2006 01:58
You'd have thought using a string would be faster - a list of 26 elements isn't hugely slow but you'd still expect it to be slower than extracting a substring from a string of equal length. If you're doing randoms with lower and upper case, digits, symbols etc. then the string gets relatively faster with each extra character you add as I understand it.
MadamG Zagato
means business
Join date: 17 Sep 2005
Posts: 1,402
01-18-2006 09:05
From: DoteDote Edison
I made a random key generator to randomly search the database for textures. So far, after about 400 keys, none have produced an actual texture (other than "Missing Image";). Anyway, the basic concept follows:

Create a list of characters, ["a", "b", "c", etc...];
Create a function that appends a random string from the list to a string variable.
Loop the function as often as needed, adding to the string each new character.

CODE
list chars = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]; //surely a better way than this
string output = "";

integer length = 1;

default {
integer x;
for(x=0;x<length;x++) {
float y = llFrand(llGetListLength(chars)) - 0.1;
output = output + llList2String(chars, (integer)y);
}
llSay(0, output);
}


Neat. However, the output is creating a pyramid like this when I set the length to 10:

Object whispers: 1
Object whispers: 1o
Object whispers: 1o8
Object whispers: 1o8g
Object whispers: 1o8gx
Object whispers: 1o8gx6
Object whispers: 1o8gx6s
Object whispers: 1o8gx6s5
Object whispers: 1o8gx6s5h
Object whispers: 1o8gx6s5h0

Can we somehow just get the last ouput string 1o8gx6s5h0 ?