The R Project SVN R

Rev

Rev 44737 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
574 ihaka 1
/*
2
 *  Mathlib : A C Library of Special Functions
3
 *  Copyright (C) 1998 Ross Ihaka
59039 ripley 4
 *  Copyright (C) 2000-2008 The R Core Team
574 ihaka 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
42302 ripley 17
 *  along with this program; if not, a copy is available at
18
 *  http://www.r-project.org/Licenses/
574 ihaka 19
 *
20
 *  DESCRIPTION
21
 *
13047 maechler 22
 *    Pseudo-random variates from a t distribution.
574 ihaka 23
 *
24
 *  NOTES
25
 *
26
 *    This function calls rchisq and rnorm to do the real work.
27
 */
28
 
8431 ripley 29
#include "nmath.h"
574 ihaka 30
 
31
double rt(double df)
32
{
7725 ripley 33
    if (ISNAN(df) || df <= 0.0)	ML_ERR_return_NAN;
7671 maechler 34
 
5108 maechler 35
    if(!R_FINITE(df))
7725 ripley 36
	return norm_rand();
13153 ripley 37
    else {
38
/* Some compilers (including MW6) evaluated this from right to left
39
	return norm_rand() / sqrt(rchisq(df) / df); */
44737 maechler 40
	double num = norm_rand();
13153 ripley 41
	return num / sqrt(rchisq(df) / df);
42
    }
574 ihaka 43
}