Difference between revisions of "RandomString"
Jump to navigation
Jump to search
Ugleh Ulrik (talk | contribs) (Creation of RandomString function) |
m (<lsl> tag to <source>) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 7: | Line 7: | ||
|func_footnote= | |func_footnote= | ||
See also: [[String]] | See also: [[String]] | ||
|spec=< | |spec=<source lang="lsl2">//This version provides random numbers and letters | ||
string RandomString(integer length) { | string RandomString(integer length) { | ||
list characters = ["0","1","2","3","4","5","6","7","8","9","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"]; | list characters = ["0","1","2","3","4","5","6","7","8","9","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"]; | ||
Line 17: | Line 17: | ||
while(length > ++p); | while(length > ++p); | ||
return emp; | return emp; | ||
}</ | }</source> | ||
< | <source lang="lsl2">//This version is only strings | ||
string RandomString(integer length) { | string RandomString(integer length) { | ||
list characters = ["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"]; | list characters = ["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"]; | ||
Line 30: | Line 30: | ||
return emp; | return emp; | ||
} | } | ||
</ | </source> | ||
Because strings held in lists occupy an overhead of 18 bytes per string, the above functionality can be written using less bytecode space by holding | |||
the alphabet in one string and extracting letters with llGetSubString(). The following user defined function is about 200 bytes shorter in Mono ByteCode than the previous UDF. | |||
<source lang="lsl2"> | |||
string RandomString(integer length) | |||
{ | |||
string characters = "abcdefghijklmnopqrstuvwxyz"; | |||
string emp; | |||
integer p; | |||
integer q; | |||
do | |||
{ | |||
q = (integer) llFrand(26); | |||
emp += llGetSubString(characters, q, q); | |||
} | |||
while(++p < length); | |||
return emp; | |||
} | |||
</source> | |||
|caveats=*The random [[String]] is created using the list, if you want to restrict some letters, or add more characters just add to the list. | |caveats=*The random [[String]] is created using the list, if you want to restrict some letters, or add more characters just add to the list. | ||
|examples=< | |examples=<source lang="lsl2"> | ||
default | default | ||
{ | { | ||
Line 41: | Line 60: | ||
} | } | ||
//Says something like fvl3d2zv4z</ | //Says something like fvl3d2zv4z</source> | ||
|helpers | |helpers | ||
|notes | |notes | ||
Line 47: | Line 67: | ||
|also_functions | |also_functions | ||
|also_articles | |also_articles | ||
|cat1= | |cat1=User-Defined_Functions | ||
|cat2 | |cat2=Examples | ||
|cat3 | |cat3 | ||
|cat4 | |cat4 | ||
}} | }} |
Latest revision as of 14:32, 22 January 2015
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Summary
Function: string RandomString( integer length );Returns a string that is made up of random letters and numbers based on the length given.
• integer | length | – | List of Random String |
See also: String
Specification
//This version provides random numbers and letters
string RandomString(integer length) {
list characters = ["0","1","2","3","4","5","6","7","8","9","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"];
string emp;
integer p;
do{
emp += llList2String(characters, llRound(llFrand(llGetListLength(characters) - 0 + 1 )));
}
while(length > ++p);
return emp;
}
//This version is only strings
string RandomString(integer length) {
list characters = ["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"];
string emp;
integer p;
do{
emp += llList2String(characters, llRound(llFrand(llGetListLength(characters) - 0 + 1 )));
}
while(length > ++p);
return emp;
}
Because strings held in lists occupy an overhead of 18 bytes per string, the above functionality can be written using less bytecode space by holding the alphabet in one string and extracting letters with llGetSubString(). The following user defined function is about 200 bytes shorter in Mono ByteCode than the previous UDF.
string RandomString(integer length)
{
string characters = "abcdefghijklmnopqrstuvwxyz";
string emp;
integer p;
integer q;
do
{
q = (integer) llFrand(26);
emp += llGetSubString(characters, q, q);
}
while(++p < length);
return emp;
}
Caveats
- The random String is created using the list, if you want to restrict some letters, or add more characters just add to the list.
Examples
default
{
touch_start(integer total_number)
{
llSay(0, RandomString(10));
}
}
//Says something like fvl3d2zv4z