The R Project SVN R

Rev

Rev 13047 | Rev 42302 | 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
13047 maechler 4
 *  Copyright (C) 2000-2001 The R Development 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
17
 *  along with this program; if not, write to the Free Software
5458 ripley 18
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
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
{
13153 ripley 33
    double num;
7725 ripley 34
    if (ISNAN(df) || df <= 0.0)	ML_ERR_return_NAN;
7671 maechler 35
 
5108 maechler 36
    if(!R_FINITE(df))
7725 ripley 37
	return norm_rand();
13153 ripley 38
    else {
39
/* Some compilers (including MW6) evaluated this from right to left
40
	return norm_rand() / sqrt(rchisq(df) / df); */
41
	num = norm_rand();
42
	return num / sqrt(rchisq(df) / df);
43
    }
574 ihaka 44
}