ID:195038
 
//Title: Random Code Generator (Alphanumeric)
//Contributed by: Haywire


/*
Basic use of BYOND's copytext() function in combination with a loop and a variable to return a set of alphanumeric characters in random order. This was inspired when I started to build mine for the activate-code system for my website, although my one is built in PHP and goes beyond alphanumeric characters.
*/

proc
GenerateCode(var/length)
var lookFrom = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"// the string of alphanumeric characters
for(var/i=1;i<=length;i++)// looping through each char until the length size has been met
var/random = rand(1,length(lookFrom))// gets a random number from 1 and the length of the string lookFrom
. += copytext(lookFrom,random,random+1)// adding the randomly selected char to the return variable


--Haywire
Your way is a misuse of loops and strings, using ASCII number codes is a much better way for this.

// Title: Random String Generator
// Contributed by: Metamorphman

/* This code genrator, aprat from being fleixble, is actually rather surprisingly fast.
It's based on using bitflags to determine what chracters can be used in the returned string,
then picking codes from a pool for adding them to the returned value.

*/


#define TEXT 1
#define INTEGER 2
#define SYMBOL 4

proc/randString( length = 0 , flags = TEXT|INTEGER )

var/l[0]
if( flags & TEXT )
l += list( list( 65 , 90 ) )
l += list( list( 97 , 122 ) )

if( flags & INTEGER )
l += list( list( 48 , 57 ) )

if( flags & SYMBOL )
l += list( list( 33 , 47 ) )
l += list( list( 162 , 254 ) )

for( var/_ = 1 to length )
var/x = pick( l )
. += ascii2text( rand( x[1] , x[2] ) )
In response to Metamorphman
Lists are not needed. You can use relative probability in pick().

// Title: GenTxt
// Contributed by: Jemai1

// GenTxt generates random text with specified length and characters.

#define LOWERTEXT 1
#define UPPERTEXT 2
#define TEXT (LOWERTEXT|UPPERTEXT)
#define INTEGER 4

proc
GenTxt(n=1,flag=TEXT|INTEGER) // defaults: 1, alphanumeric (UPPERTEXT|LOWERTEXT|INTEGER)

if(!(flag&(TEXT|INTEGER))) // no supported bitflag
return //early escape

while(n-->=1) // consider n's floor value
.+=ascii2text(
pick(
flag&INTEGER&&10 ; rand(48,57) , // flag&INTEGER ? 10 : 0 ; rand(48,57)
flag&UPPERTEXT&&26 ; rand(65,90) ,
flag&LOWERTEXT&&26 ; rand(97,122)
))