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()
```

On Mon, May 11, 2020 at 5:20 PM Joe Wicentowski <joewiz@gmail.com> wrote:
Hi all,

I encountered a stack overflow error with the following code, which is designed to return as many random numbers as the user requests.  BaseX's error suggested, "Try tail recursion?"  I think I am already using tail recursion.  Changing the $n variable from 1407 to 1406 or lower avoids the error.  Any number 1407 or higher triggers the error.

Thank you for any hints!
Joe

```
xquery version "3.1";

declare function local:get-random-numbers($how-many as xs:integer, $generator as map(*)) {
    if ($how-many ge 1) then
        (
            $generator?number,
            local:get-random-numbers($how-many - 1, $generator?next())
        )
    else
        ()
};

declare function local:get-random-numbers($how-many as xs:integer) {
    local:get-random-numbers($how-many, random-number-generator())
};

let $n := 1407
return
    local:get-random-numbers($n) => count()
```