Script doesn't work with multiple digit numbers, doesn't include some

Been working on this script as a coding exercise:

display dialog "digits" default answer ""
set digitsnumber to text returned of result

display dialog "places" default answer ""
set placesnumber to text returned of result



set digitsarray to {}

set astidmem to AppleScript's text item delimiters

set repeatcount to 0


set AppleScript's text item delimiters to " "

set placesarray to {}
repeat with i from (count every text item of placesnumber) to 1 by -1
	set beginning of placesarray to text item i of placesnumber
end repeat

set AppleScript's text item delimiters to "-"

set xToY to {}
set toDelete to {}
set individualItems to {}

repeat with i from (count of items of placesarray) to 1 by -1
	if (count of every text item of item i of placesarray) > 1 then
		set beginning of xToY to item i of placesarray as text
		set beginning of toDelete to item i of placesarray
	end if
end repeat
repeat with i from 1 to count of items of placesarray
	if item i of placesarray is not in toDelete then set end of individualItems to item i of placesarray
end repeat


repeat until (count of items of digitsarray) ≥ digitsnumber
	set randomfunction to random number
	
	if randomfunction > 0.5 then
		repeat
			if (count of items of individualItems) = 0 then
				set repeatcount to repeatcount + 1
				exit repeat
			end if
			set repeatcount to repeatcount + 1
			set randomtext to random number from 1 to (count of items of individualItems) div 1
			set placesnumber2 to 1 / (10 ^ (item randomtext of individualItems))
			set newdigit to ((random number) div placesnumber2) as text
			if ((count newdigit) as text) is equal to (text item randomtext of individualItems) then
				set end of digitsarray to " " & newdigit
				exit repeat
			end if
		end repeat
	end if
	
	if randomfunction ≤ 0.5 then
		repeat
			if (count of items of xToY) = 0 then
				set repeatcount to repeatcount + 1
				exit repeat
			end if
			set repeatcount to repeatcount + 1
			set setnumber to random number from 1 to count of items of xToY
			set numlength to ((random number from first text item of item setnumber of xToY to last text item of item setnumber of xToY) div 1) as text
			set placesnumber2 to 1 / (10 ^ numlength)
			set newdigit to ((random number) div placesnumber2) as text
			if ((count newdigit) as text) is equal to numlength then
				set end of digitsarray to " " & newdigit
				exit repeat
			end if
		end repeat
	end if
	
end repeat

set AppleScript's text item delimiters to astidmem

display dialog digitsarray as text

return "repeatcount:" & repeatcount & "individualItems:" & individualItems & "xToY:" & xToY

You input how many numbers you want it to output and how many digits you want in the numbers in the format ‘1 3 5-8’ so it will output numbers 1, 3, 5, 6, 7, and 8 digits long. One of the problems is when you put in something such as 5-8, it only outputs numbers 5-7 digits long. Maybe having one with 8 digits is just very rare, all I really know is I haven’t seen any after trying that with hundreds of numbers. The other problem I’m having is that you can’t input the numbers to have more than 9 places. I can’t really see why the code doesn’t allow for this, but it just keeps running. I created this code entirely on my own, other than basic help on how to use certain functions online. I’m fairly proud of it, trying to look at it and remember exactly how it works while I was changing it certainly put a strain on my brain. Any help with these 2 problems would be appreciated. Thanks

Hi, kyle1320

Your script’s very hard to read and analyse because your dialogs and variable names contain ‘digit’ or ‘text’ when you mean number, ‘number’ where you mean text, and ‘places’ when you mean digits!

However, in this line .

set numlength to ((random number from first text item of item setnumber of xToY to last text item of item setnumber of xToY) div 1) as text

. if the item in xToY is “5-8”, the effectual action is

set numlength to (random number from "5" to "8") as text

The ‘random number’ function’s getting confused by the fact that the range parameters are text and not integers and is returning a real result instead of an integer. Your ‘div 1’ merely truncates this, so you rarely, if ever, get the highest number in the range. You should coerce the texts to integers before passing them to the function. Crudely:

set numlength to ((random number from ((first text item of item setnumber of xToY) as integer) to (last text item of item setnumber of xToY) as integer)) as text

As for your other problem, you’re coercing numbers to text in order to count the digits and display the results. Somewhere in the range of nine-digit integers, AppleScript begins to display numbers ” and coerce them to text ” in exponential format:

123456789 as text --> "123456789"
-- But:
987654321 as text --> "9.87654321E+8"

Once you get into this higher range of numbers, your script isn’t going to work properly. You’ll either have to write a number-to-string handler or build up the numbers purely textually from the individual digits. The latter’s very easy to do:

set numberOfDigits to 12 -- (for instance)

set numberText to (random number from 1 to 9) as text -- So that we don't start with "0"
repeat (numberOfDigits - 1) times
	set numberText to numberText & some character of "0123456789"
end repeat