Random String

on randomString(aLength)
	set randomChars to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "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"}
	set aString to ""
	
	repeat aLength times
		set aString to aString & some item of randomChars
	end repeat
	
	return aString
end randomString

-- Example
randomString(8)

Using terms from yours, Bruce, I’ve attached the one I use:


on randomString(aLength, Unique)
	set randomChars to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "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"}
	set aString to ""
	
	if aLength > 36 and Unique then
		if (button returned of (display dialog "Unique characters not possible for that length." & return & "Do you want non-unique characters?") is "OK") then set Unique to false
	end if

	if Unique then
		repeat until (count aString) = aLength
			set I to some item of randomChars
			if I is not in aString then set aString to aString & I
		end repeat
	else
		repeat aLength times
			set aString to aString & some item of randomChars
		end repeat
	end if
	
	return aString
end randomString

-- Example
{randomString(20, true), randomString(20, false)}