Game Development Community

dev|Pro Game Development Curriculum

Random String Maker

by XD · 10/23/2008 (11:24 am) · 3 comments

This code could of been better, but it get's the job done.

function getRandomString()
{
	$string = "";
	$Letters = "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";
	for(%i=0; %i <= 26; %i++)
	{
		$letter[%i] = getWord($Letters, %i);  	
	}
	for(%z=0; %z <= 15; %z++) {
	$random = getRandom(0, 26);
	$string = $string @ $letter[$random];
	}
	return $string;
}

#1
10/23/2008 (9:57 pm)
I would change all of the global variables to local ones, and add a parameter to the function.

Edit-> Like this:
function getRandomString(%count)
{
	if(%count $= "")
		%count = 1;
	%string = "";
	%Letters = "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";
	for(%i=0; %i <= 26; %i++)
	{
		%letter[%i] = getWord(%Letters, %i);
  	}
	for(%z=0; %z <= %count; %z++)
	{
	%random = getRandom(0, 26);
	%string = %string @ %letter[$random];
	}
	return %string;
}
#2
10/24/2008 (12:26 am)
This could be advanced into something like this:
function generatePassword(%len, %needCase)
{
   %allChars = "abcdefghijklmnopqrstuvwxyz1234567890";
   %pass = "";
   for(%i=0;%i<%len;%i++)
   {
      %index = getRandom(0, StrLen(%allChars)-1);
      %char = getSubStr(%allChars, %index, 1);
      if (%needCase && getRandom(0,1)==1)
         %char = strUpr(%char);
      %pass = %pass @ %char;
   }
   return %pass;
}
#3
11/29/2008 (10:42 pm)
How do I apply this in Random String Array?

The String is in Array for example:

a[0] = "good morning";
a[1] = "thank you";
a[2] = "welcome";
a[3] = "how are you";