# [R], 66 bytes

<!-- language-all: lang-r -->

 f=function(n,x=sample(seq(n)-1,1)){cat(n," ");"if"(x,f(x),cat(0))}

[Try it online!][TIO-jje8y2l1]

[R]: https://www.r-project.org/
[TIO-jje8y2l1]: https://tio.run/##FclJCoAwDADAr0hOCURIz9LHlNKAoHFphYL49qjXmdNdo16W27wZGvdY07ovBWs50GgMHIjunNp3MABNMCtgZ8VO/LMQPa4YRIT8BQ "R – Try It Online"

Should be able to beat this with a `while` loop.