# Truncated chi-square returning NaNs

**URL:** https://discourse.julialang.org/t/truncated-chi-square-returning-nans/100187
**Category:** Statistics
**Created:** [June 11, 2023, 2:48pm UTC](https://discourse.julialang.org/t/truncated-chi-square-returning-nans/100187 "2023-06-11T14:48:42Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Jad\_Zeitouni](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jad_zeitouni/32/50678_2.png) [@Jad\_Zeitouni](https://discourse.julialang.org/u/Jad_Zeitouni)
#### Post date: [June 11, 2023, 2:48pm UTC](https://discourse.julialang.org/t/truncated-chi-square-returning-nans/100187/1 "2023-06-11T14:48:42Z")

</div>

I’m trying to sample from a truncated chi-square distribution, defined over [0, a], by using the following line of code.

```julia
rand(Truncated(Chisq(n), 0, a))

```

This does alright most of the time, but sometimes it returns NaNs, for example with n = 1595 and a = 250. Interestingly enough, it returns valid values for smaller values of a (e.g. a = 5).

Is there anyway around this behavior? What is causing it?
