I found a version that works. In the original version I must not have been using tail recursion correctly. Here is the version that works. Can anyone explain why the first version doesn't properly trigger tail recursion?
```
xquery version "3.1";
declare function local:get-random-numbers($how-many as xs:integer, $generator as map(*), $accumulator) {
if ($how-many ge 1) then
local:get-random-numbers($how-many - 1, $generator?next(), ($accumulator, $generator?number))
else
$accumulator
};
declare function local:get-random-numbers($how-many as xs:integer) {
local:get-random-numbers($how-many, random-number-generator(), ())
};
let $n := 2000
return
local:get-random-numbers($n) => count()
```