@@ -274,6 +274,47 @@ impl IndependentSample<f64> for FisherF {
274274 }
275275}
276276
277+ /// The Student t distribution, `t(nu)`, where `nu` is the degrees of
278+ /// freedom.
279+ ///
280+ /// # Example
281+ ///
282+ /// ```rust
283+ /// use std::rand;
284+ /// use std::rand::distributions::{StudentT, IndependentSample};
285+ ///
286+ /// fn main() {
287+ /// let t = StudentT::new(11.0);
288+ /// let v = t.ind_sample(&mut rand::task_rng());
289+ /// println!("{} is from a t(11) distribution", v)
290+ /// }
291+ /// ```
292+ pub struct StudentT {
293+ priv chi: ChiSquared ,
294+ priv dof: f64
295+ }
296+
297+ impl StudentT {
298+ /// Create a new Student t distribution with `n` degrees of
299+ /// freedom. Fails if `n <= 0`.
300+ pub fn new ( n : f64 ) -> StudentT {
301+ assert ! ( n > 0.0 , "StudentT::new called with `n <= 0`" ) ;
302+ StudentT {
303+ chi : ChiSquared :: new ( n) ,
304+ dof : n
305+ }
306+ }
307+ }
308+ impl Sample < f64 > for StudentT {
309+ fn sample < R : Rng > ( & mut self , rng : & mut R ) -> f64 { self . ind_sample ( rng) }
310+ }
311+ impl IndependentSample < f64 > for StudentT {
312+ fn ind_sample < R : Rng > ( & self , rng : & mut R ) -> f64 {
313+ let norm = * rng. gen :: < StandardNormal > ( ) ;
314+ norm * ( self . dof / self . chi . ind_sample ( rng) ) . sqrt ( )
315+ }
316+ }
317+
277318#[ cfg( test) ]
278319mod test {
279320 use rand:: * ;
@@ -323,6 +364,16 @@ mod test {
323364 f. ind_sample ( & mut rng) ;
324365 }
325366 }
367+
368+ #[ test]
369+ fn test_t ( ) {
370+ let mut t = StudentT :: new ( 11.0 ) ;
371+ let mut rng = task_rng ( ) ;
372+ for _ in range ( 0 , 1000 ) {
373+ t. sample ( & mut rng) ;
374+ t. ind_sample ( & mut rng) ;
375+ }
376+ }
326377}
327378
328379#[ cfg( test) ]
0 commit comments