1 /*
2  *  Mathlib : A C Library of Special Functions
3  *  Copyright (C) 1998 Ross Ihaka
4  *  Copyright (C) 2000-2008 The R Core Team
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, a copy is available at
18  *  http://www.r-project.org/Licenses/
19  *
20  *  DESCRIPTION
21  *
22  *    Pseudo-random variates from a t distribution.
23  *
24  *  NOTES
25  *
26  *    This function calls rchisq and rnorm to do the real work.
27  */
28 
29 #include "nmath.h"
30 
rt(double df)31 double rt(double df)
32 {
33     if (ISNAN(df) || df <= 0.0)	ML_ERR_return_NAN;
34 
35     if(!R_FINITE(df))
36 	return norm_rand();
37     else {
38 /* Some compilers (including MW6) evaluated this from right to left
39 	return norm_rand() / sqrt(rchisq(df) / df); */
40 	double num = norm_rand();
41 	return num / sqrt(rchisq(df) / df);
42     }
43 }
44