Generating random strings of characters

is this possible? tried several things but none seem to work any help is greatly appreciated. :lol:

There are several approaches to this, for example:

set validChars to {"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 randomChar to some item of validChars

-- or maybe:

set randomChar to ASCII character (random number from 97 to 122)

The first approach has the advantage that it’s easy to restrict the list to specific characters, but can involve a lot of typing. The second method is quicker, but requires that you know the ascii value of the characters you want, and is harder to include a non-contiguous range of ascii characters (e.g. upper and lower case characters, or digits and lower case)

The Suggestions should work:

Try something like the following for validation:


set randomString to ""

repeat with x from 1 to 20
	set randomChar to ASCII character (random number from 97 to 122)
	set randomString to randomString & randomChar
end repeat

display dialog randomString

If you want uppercase letters, you can change the ASCII range to be between 65 and 90. (In the Terminal, type man ascii and look at the Decimal set.)

I’m sure there is a cleaner way of doing this but if you want a more robust way to generate random strings, try this handler:

set some_string to my return_random_string(true, false, false, true, false, 8)

on return_random_string(include_capitals, include_lowers, include_diacriticals, include_numbers, include_nonalphanumerics, string_length)
	set the_captials to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	if include_diacriticals then set the_captials to the_captials & "                    ?"
	set the_lowers to "abcdefghijklmnopqrstuvwxyz"
	if include_diacriticals then set the_lowers to the_lowers & "                    ??"
	set the_numbers to "0123456789"
	set random_pool to {}
	if include_capitals then set end of random_pool to the_captials
	if include_lowers then set end of random_pool to the_lowers
	if include_numbers then set end of random_pool to the_numbers
	if include_nonalphanumerics then
		--this is because the high ASCII chars won't come through in the post:
		repeat with this_num in {33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 58, 59, 60, 61, 62, 63, 64, 91, 92, 93, 94, 95, 96, 123, 124, 125, 126, 127, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 208, 209, 210, 211, 212, 213, 214, 215, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 240, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255}
			set end of random_pool to ASCII character this_num
		end repeat
	end if
	set random_pool to characters of (random_pool as string)
	set return_string to {}
	repeat string_length times
		set end of return_string to (item (random number from 1 to (count random_pool)) of random_pool)
	end repeat
	return return_string as string
end return_random_string

Jon