Since you're using a while loop I'd guess you have an infinite loop somehow.
One thing I notice is if 9 is ever needed it won't work since
floor(random(1,9)) will only give values from 1 to 8
Why?
random(n) will only give values from 0 up to but not including n.
Instead you should do floor(random(1,10)) if you want 9 included.