generate codes x times

I want to generate a list of codes. The amount of codes depends on a value given in a display dialog.
There shouldn’t be duplicate codes. The list is displayed in text editor, each code on a seperate line.

I found a script on this forum, but i dont know how to let it generate the codes x times. Plus how to display the result in text editor?

Thx for helping me out

set quantityInput to display dialog "How many numbers do you want" default answer "100" buttons {"Generate"} default button 1
set quantity to the text returned of quantityInput

-- above isn't linked to below. The script below only generates one code. I dont know how... The final result needs to be displayed in text editor.

on randomString(aLength)
	set randomChars to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "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

-- Is it possible to eliminate duplicate codes?

randomString(6)

Hi,

try this, the result is in the variable codes


set codes to {}
set {text returned:codesQuantity} to display dialog "How many codes do you want" default answer "100" buttons {"OK"} default button 1
try
	set codesQuantity to codesQuantity as integer
	set {text returned:numbersQuantity} to display dialog "Enter the charcter length of one code" default answer "5" buttons {"Generate"} default button 1
	set numbersQuantity to numbersQuantity as integer
	set x to 0
	repeat while x < codesQuantity
		set newCode to randomString(numbersQuantity)
		if codes does not contain newCode then
			set end of codes to newCode
			set x to x + 1
		end if
	end repeat

on error
	display dialog "please enter only numbers" buttons {"Cancel"} default button 1
end try

on randomString(aLength)
	set randomChars to "123456789ABCDEFGHIJKLMNPRSTUVWXYZ"
	set aString to ""
	
	repeat aLength times
		set aString to aString & some character of randomChars
	end repeat
	return aString
end randomString

thx for replying, but it doesn’t seems to be working. It only shows codesQuantity in the result field…

add codes before the on error line then you’ll see the result in the result window

ah ok
thx for the help