Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

randgen.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002    Major portions of this software are copyrighted by the Medical College
00003    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00004    License, Version 2.  See the file README.Copyright for details.
00005 ******************************************************************************/
00006 
00007 /*---------------------------------------------------------------------------*/
00008 /*
00009   Some utility routines for generating random numbers.
00010 
00011   File:    randgen.c
00012   Author:  B. Douglas Ward
00013   Date:    15 February 1999
00014 
00015   Mod:     Correction to rand_normal
00016   Date:    15 September 1999
00017 
00018   Mod:     Added routine rand_initialize.
00019   Date:    28 January 2000
00020 */
00021 
00022 
00023 /*---------------------------------------------------------------------------*/
00024 /*
00025   Initialize the random number generator.
00026 */
00027 
00028 void rand_initialize (long int seedval)
00029 {
00030   srand48 (seedval);
00031 }
00032 
00033 
00034 /*---------------------------------------------------------------------------*/
00035 /*
00036   Routine to generate a uniform U(a,b) random variate.
00037 */
00038 
00039 float rand_uniform (float a, float b)
00040 {
00041   return (a + (float)drand48() * (b-a) );
00042 }
00043 
00044 
00045 /*---------------------------------------------------------------------------*/
00046 /*
00047   Routine to generate a normal N(mu,var) random variate.
00048 */
00049 
00050 float rand_normal (float mu, float var)
00051 {
00052   float u1, u2;
00053   float r, n;
00054 
00055 
00056   u1 = 0.0;
00057   while (u1 <= 0.0)
00058     {
00059       u1 = rand_uniform (0.0, 1.0);
00060     }
00061   u2 = rand_uniform (0.0, 1.0);
00062 
00063   r = sqrt(-2.0*log(u1));
00064   n = r * cos(2.0*PI*u2);
00065 
00066   return (mu + n * sqrt(var));
00067 }
00068 
00069 
00070 /*---------------------------------------------------------------------------*/
00071 /*
00072   Routine to generate two independent normal N(mu,var) random variates.
00073 */
00074 
00075 void rand_binormal (float mu, float var, float * n1, float * n2)
00076 {
00077   float u1, u2;
00078   float r, sigma;
00079 
00080 
00081   u1 = 0.0;
00082   while (u1 <= 0.0)
00083     {
00084       u1 = rand_uniform (0.0, 1.0);
00085     }
00086   u2 = rand_uniform (0.0, 1.0);
00087 
00088   r   = sqrt(-2.0*log(u1));
00089   sigma = sqrt (var);
00090 
00091   *n1 = mu + r * cos(2.0*PI*u2) * sigma;
00092   *n2 = mu + r * sin(2.0*PI*u2) * sigma;
00093 }
00094 
00095 
00096 /*---------------------------------------------------------------------------*/
00097 
00098 
00099 
 

Powered by Plone

This site conforms to the following standards: