/*************************    Program 4.6    ****************************/
/*                                                                      */
/************************************************************************/
/* Please Note:                                                         */
/*                                                                      */
/* (1) This computer program is written by Tao Pang in conjunction with */
/*     his book, "An Introduction to Computational Physics," published  */
/*     by Cambridge University Press in 1997.                           */
/*                                                                      */
/* (2) No warranties, express or implied, are made for this program.    */
/*                                                                      */
/************************************************************************/

#include <stdio.h>
#include <math.h>
#define NMAX 100

void rmsg (n,xs,a)
/* Subroutine for generating a random matrix in the Gaussian
   orthogonal ensemble with XS as the standard deviation of 
   the off-diagonal elements.  Copyright (c) Tao Pang 1997. */
int n;
double xs;
double a[NMAX][NMAX];
{
int i,j;
double g1,g2;
void grnf();

for (i = 0; i < n; ++i)
  {
  grnf(&g1,&g2);
  a[i][i] = sqrt(2.0)*g1*xs;
  }

for (i = 0; i < n; ++i)
  {
  for (j = i+1; j < n; ++j)
    {
    grnf (&g1,&g2);
    a[i][j] = g1*xs;
    a[j][i] = a[i][j];
    }
  }
}

void grnf (x,y)
/* Two Gaussian random numbers generated from two uniform
   random numbers.  Copyright (c) Tao Pang 1997. */
double *x,*y;
{
double pi,r1,r2;
double ranf();

pi =  4*atan(1);
r1 = -log(1-ranf());
r2 =  2*pi*ranf();
r1 =  sqrt(2*r1);
*x  = r1*cos(r2);
*y  = r1*sin(r2);
}

double ranf()
/* Uniform random number generator x(n+1)= a*x(n) mod c
   with a = pow(7,5) and c = pow(2,31)-1.
   Copyright (c) Tao Pang 1997. */
{
const int ia=16807,ic=2147483647,iq=127773,ir=2836;
int il,ih,it;
double rc;
extern int iseed;
ih = iseed/iq;
il = iseed%iq;
it = ia*il-ir*ih;
if (it > 0)
  {
  iseed = it;
  }
else
  {
iseed = ic+it;
  }
rc = ic;
return iseed/rc;
}
