0% found this document useful (0 votes)
97 views

Normal CPP

This document contains C++ code for generating pseudorandom numbers from normal and uniform distributions. It includes functions to return complex and real values from normal distributions with mean 0 and variance 1, as well as from normal distributions with custom means and variances. It also includes functions for returning integer values from normal distributions. The code comments explain the purpose and parameters of each function.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Normal CPP

This document contains C++ code for generating pseudorandom numbers from normal and uniform distributions. It includes functions to return complex and real values from normal distributions with mean 0 and variance 1, as well as from normal distributions with custom means and variances. It also includes functions for returning integer values from normal distributions. The code comments explain the purpose and parameters of each function.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 33

#

#
#
#
#
#
#

include
include
include
include
include
include
include

<complex>
<cstdlib>
<iostream>
<iomanip>
<fstream>
<ctime>
<cmath>

using namespace std;


# include "normal.hpp"
//****************************************************************************80
complex <float> c4_normal_01 ( int &seed )
//****************************************************************************80
//
// Purpose:
//
//
C4_NORMAL_01 returns a unit pseudonormal C4.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
09 April 2012
//
// Author:
//
//
John Burkardt
//
// Parameters:
//
//
Input/output, int &SEED, a seed for the random number generator.
//
//
Output, complex <float> C4_NORMAL_01, a unit pseudonormal value.
//
{
const float r4_pi = 3.141592653589793;
float v1;
float v2;
complex <float> value;
float x_c;
float x_r;
v1 = r4_uniform_01 ( seed );
v2 = r4_uniform_01 ( seed );
x_r = sqrt ( - 2.0 * log ( v1 ) ) * cos ( 2.0 * r4_pi * v2 );
x_c = sqrt ( - 2.0 * log ( v1 ) ) * sin ( 2.0 * r4_pi * v2 );
value = complex <float> ( x_r, x_c );
return value;
}
//****************************************************************************80

complex <double> c8_normal_01 ( int &seed )


//****************************************************************************80
//
// Purpose:
//
//
C8_NORMAL_01 returns a unit pseudonormal C8.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
09 April 2012
//
// Author:
//
//
John Burkardt
//
// Parameters:
//
//
Input/output, int &SEED, a seed for the random number generator.
//
//
Output, complex <double> C8_NORMAL_01, a unit pseudonormal value.
//
{
const double r8_pi = 3.141592653589793;
double v1;
double v2;
complex <double> value;
double x_c;
double x_r;
v1 = r8_uniform_01 ( seed );
v2 = r8_uniform_01 ( seed );
x_r = sqrt ( - 2.0 * log ( v1 ) ) * cos ( 2.0 * r8_pi * v2 );
x_c = sqrt ( - 2.0 * log ( v1 ) ) * sin ( 2.0 * r8_pi * v2 );
value = complex <double> ( x_r, x_c );
return value;
}
//****************************************************************************80
int i4_normal_ab ( float a, float b, int &seed )
//****************************************************************************80
//
// Purpose:
//
//
I4_NORMAL_AB returns a scaled pseudonormal I4.
//
// Discussion:
//
//
The normal probability distribution function (PDF) is sampled,
//
with mean A and standard deviation B.
//
// Licensing:

//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
09 April 2012
//
// Author:
//
//
John Burkardt
//
// Parameters:
//
//
Input, float A, the mean of the PDF.
//
//
Input, float B, the standard deviation of the PDF.
//
//
Input/output, int &SEED, a seed for the random number generator.
//
//
Output, int I4_NORMAL_AB, a sample of the normal PDF.
//
{
int value;
int value_float;
value_float = a + b * r4_normal_01 ( seed );
value = ( int ) ( value_float );
return value;
}
//****************************************************************************80
long long int i8_normal_ab ( double a, double b, long long int &seed )
//****************************************************************************80
//
// Purpose:
//
//
I8_NORMAL_AB returns a scaled pseudonormal I8.
//
// Discussion:
//
//
The normal probability distribution function (PDF) is sampled,
//
with mean A and standard deviation B.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
09 April 2012
//
// Author:
//
//
John Burkardt
//
// Parameters:
//

//
//
//
//
//
//
//
//
{

Input, double A, the mean of the PDF.


Input, double B, the standard deviation of the PDF.
Input/output, long long int &SEED, a seed for the random number generator.
Output, long long int I8_NORMAL_AB, a sample of the normal PDF.
int seed_int;
double value_double;
long long int value_long_long_int;
seed_int = ( int ) seed;
value_double = a + b * r8_normal_01 ( seed_int );
if ( value_double < 0.0 )
{
value_long_long_int = ( long long int ) ( value_double - 0.5 );
}
else
{
value_long_long_int = ( long long int ) ( value_double + 0.5 );
}
seed = ( long long int ) seed_int;

return value_long_long_int;
}
//****************************************************************************80
float r4_normal_01 ( int &seed )
//****************************************************************************80
//
// Purpose:
//
//
R4_NORMAL_01 returns a unit pseudonormal R4.
//
// Discussion:
//
//
The standard normal probability distribution function (PDF) has
//
mean 0 and standard deviation 1.
//
//
The Box-Muller method is used, which is efficient, but
//
generates two values at a time.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
06 August 2013
//
// Author:
//
//
John Burkardt
//

// Parameters:
//
//
Input/output, int &SEED, a seed for the random number generator.
//
//
Output, float R4_NORMAL_01, a normally distributed random value.
//
{
const float r4_pi = 3.141592653589793;
float r1;
float r2;
float x;
r1 = r4_uniform_01 ( seed );
r2 = r4_uniform_01 ( seed );
x = sqrt ( - 2.0 * log ( r1 ) ) * cos ( 2.0 * r4_pi * r2 );
return x;
}
//****************************************************************************80
float r4_normal_ab ( float a, float b, int &seed )
//****************************************************************************80
//
// Purpose:
//
//
R4_NORMAL_AB returns a scaled pseudonormal R4.
//
// Discussion:
//
//
The normal probability distribution function (PDF) is sampled,
//
with mean A and standard deviation B.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
09 April 2012
//
// Author:
//
//
John Burkardt
//
// Parameters:
//
//
Input, float A, the mean of the PDF.
//
//
Input, float B, the standard deviation of the PDF.
//
//
Input/output, int &SEED, a seed for the random number generator.
//
//
Output, float R4_NORMAL_AB, a sample of the normal PDF.
//
{
float value;
value = a + b * r4_normal_01 ( seed );

return value;
}
//****************************************************************************80
float r4_uniform_01 ( int &seed )
//****************************************************************************80
//
// Purpose:
//
//
R4_UNIFORM_01 returns a unit pseudorandom R4.
//
// Discussion:
//
//
This routine implements the recursion
//
//
seed = 16807 * seed mod ( 2^31 - 1 )
//
r4_uniform_01 = seed / ( 2^31 - 1 )
//
//
The integer arithmetic never requires more than 32 bits,
//
including a sign bit.
//
//
If the initial seed is 12345, then the first three computations are
//
//
Input
Output
R4_UNIFORM_01
//
SEED
SEED
//
//
12345 207482415 0.096616
//
207482415 1790989824 0.833995
//
1790989824 2035175616 0.947702
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
09 April 2012
//
// Author:
//
//
John Burkardt
//
// Reference:
//
//
Paul Bratley, Bennett Fox, Linus Schrage,
//
A Guide to Simulation,
//
Springer Verlag, pages 201-202, 1983.
//
//
Pierre L'Ecuyer,
//
Random Number Generation,
//
in Handbook of Simulation
//
edited by Jerry Banks,
//
Wiley Interscience, page 95, 1998.
//
//
Bennett Fox,
//
Algorithm 647:
//
Implementation and Relative Efficiency of Quasirandom
//
Sequence Generators,
//
ACM Transactions on Mathematical Software,

//
Volume 12, Number 4, pages 362-376, 1986.
//
//
Peter Lewis, Allen Goodman, James Miller,
//
A Pseudo-Random Number Generator for the System/360,
//
IBM Systems Journal,
//
Volume 8, pages 136-143, 1969.
//
// Parameters:
//
//
Input/output, int &SEED, the "seed" value. Normally, this
//
value should not be 0. On output, SEED has been updated.
//
//
Output, float R4_UNIFORM_01, a new pseudorandom variate, strictly between
//
0 and 1.
//
{
const int i4_huge = 2147483647;
int k;
float r;
k = seed / 127773;
seed = 16807 * ( seed - k * 127773 ) - k * 2836;
if ( seed < 0 )
{
seed = seed + i4_huge;
}
//
// Although SEED can be represented exactly as a 32 bit integer,
// it generally cannot be represented exactly as a 32 bit real number!
//
r = ( float ) ( seed ) * 4.656612875E-10;
return r;
}
//****************************************************************************80
void r4mat_print ( int m, int n, float a[], string title )
//****************************************************************************80
//
// Purpose:
//
//
R4MAT_PRINT prints an R4MAT.
//
// Discussion:
//
//
An R4MAT is a doubly dimensioned array of R4 values, stored as a vector
//
in column-major order.
//
//
Entry A(I,J) is stored as A[I+J*M]
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
10 September 2009

//
// Author:
//
//
John Burkardt
//
// Parameters:
//
//
Input, int M, the number of rows in A.
//
//
Input, int N, the number of columns in A.
//
//
Input, float A[M*N], the M by N matrix.
//
//
Input, string TITLE, a title.
//
{
r4mat_print_some ( m, n, a, 1, 1, m, n, title );
return;
}
//****************************************************************************80
void r4mat_print_some ( int m, int n, float a[], int ilo, int jlo, int ihi,
int jhi, string title )
//****************************************************************************80
//
// Purpose:
//
//
R4MAT_PRINT_SOME prints some of an R4MAT.
//
// Discussion:
//
//
An R4MAT is a doubly dimensioned array of R4 values, stored as a vector
//
in column-major order.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
26 June 2013
//
// Author:
//
//
John Burkardt
//
// Parameters:
//
//
Input, int M, the number of rows of the matrix.
//
M must be positive.
//
//
Input, int N, the number of columns of the matrix.
//
N must be positive.
//
//
Input, float A[M*N], the matrix.
//
//
Input, int ILO, JLO, IHI, JHI, designate the first row and
//
column, and the last row and column to be printed.

//
//
Input, string TITLE, a title.
//
{
# define INCX 5
int
int
int
int
int
int

i;
i2hi;
i2lo;
j;
j2hi;
j2lo;

cout << "\n";


cout << title << "\n";
if ( m <= 0 || n <= 0 )
{
cout << "\n";
cout << " (None)\n";
return;
}
//
// Print the columns of the matrix, in strips of 5.
//
for ( j2lo = jlo; j2lo <= jhi; j2lo = j2lo + INCX )
{
j2hi = j2lo + INCX - 1;
if ( n < j2hi )
{
j2hi = n;
}
if ( jhi < j2hi )
{
j2hi = jhi;
}
cout << "\n";
//
// For each column J in the current range...
//
// Write the header.
//
cout << " Col:
";
for ( j = j2lo; j <= j2hi; j++ )
{
cout << setw(7) << j - 1 << "
";
}
cout << "\n";
cout << " Row\n";
cout << "\n";
//
// Determine the range of the rows in this strip.
//
if ( 1 < ilo )
{
i2lo = ilo;
}
else
{
i2lo = 1;

}
if ( ihi < m )
{
i2hi = ihi;
}
else
{
i2hi = m;
}
for ( i = i2lo; i <= i2hi; i++ )
{
//
// Print out (up to) 5 entries in row I, that lie in the current strip.
//
cout << setw(5) << i - 1 << ": ";
for ( j = j2lo; j <= j2hi; j++ )
{
cout << setw(12) << a[i-1+(j-1)*m] << " ";
}
cout << "\n";
}
}
return;
# undef INCX
}
//****************************************************************************80
void r4vec_print ( int n, float a[], string title )
//****************************************************************************80
//
// Purpose:
//
//
R4VEC_PRINT prints an R4VEC.
//
// Discussion:
//
//
An R4VEC is a vector of R4's.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
16 August 2004
//
// Author:
//
//
John Burkardt
//
// Parameters:
//
//
Input, int N, the number of components of the vector.
//
//
Input, float A[N], the vector to be printed.
//
//
Input, string TITLE, a title.

//
{
int i;
cout << "\n";
cout << title << "\n";
cout << "\n";
for ( i = 0; i < n; i++ )
{
cout << " " << setw(8) << i
<< ": " << setw(14) << a[i] << "\n";
}
return;
}
//****************************************************************************80
float *r4vec_uniform_01_new ( int n, int &seed )
//****************************************************************************80
//
// Purpose:
//
//
R4VEC_UNIFORM_01_NEW returns a new unit pseudorandom R4VEC.
//
// Discussion:
//
//
This routine implements the recursion
//
//
seed = ( 16807 * seed ) mod ( 2^31 - 1 )
//
u = seed / ( 2^31 - 1 )
//
//
The integer arithmetic never requires more than 32 bits,
//
including a sign bit.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
04 March 2015
//
// Author:
//
//
John Burkardt
//
// Reference:
//
//
Paul Bratley, Bennett Fox, Linus Schrage,
//
A Guide to Simulation,
//
Second Edition,
//
Springer, 1987,
//
ISBN: 0387964673,
//
LC: QA76.9.C65.B73.
//
//
Bennett Fox,
//
Algorithm 647:
//
Implementation and Relative Efficiency of Quasirandom
//
Sequence Generators,

//
ACM Transactions on Mathematical Software,
//
Volume 12, Number 4, December 1986, pages 362-376.
//
//
Pierre L'Ecuyer,
//
Random Number Generation,
//
in Handbook of Simulation,
//
edited by Jerry Banks,
//
Wiley, 1998,
//
ISBN: 0471134031,
//
LC: T57.62.H37.
//
//
Peter Lewis, Allen Goodman, James Miller,
//
A Pseudo-Random Number Generator for the System/360,
//
IBM Systems Journal,
//
Volume 8, Number 2, 1969, pages 136-143.
//
// Parameters:
//
//
Input, int N, the number of entries in the vector.
//
//
Input/output, int &SEED, a seed for the random number generator.
//
//
Output, float R4VEC_UNIFORM_01_NEW[N], the vector of pseudorandom values.
//
{
int i;
int i4_huge = 2147483647;
int k;
float *r;
if ( seed == 0 )
{
cerr << "\n";
cerr << "R4VEC_UNIFORM_01_NEW - Fatal error!\n";
cerr << " Input value of SEED = 0.\n";
exit ( 1 );
}
r = new float[n];
for ( i = 0; i < n; i++ )
{
k = seed / 127773;
seed = 16807 * ( seed - k * 127773 ) - k * 2836;
if ( seed < 0 )
{
seed = seed + i4_huge;
}
r[i] = ( float ) ( seed ) * 4.656612875E-10;
}
return r;
}
//****************************************************************************80
double r8_normal_01 ( int &seed )

//****************************************************************************80
//
// Purpose:
//
//
R8_NORMAL_01 returns a unit pseudonormal R8.
//
// Discussion:
//
//
The standard normal probability distribution function (PDF) has
//
mean 0 and standard deviation 1.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
06 August 2013
//
// Author:
//
//
John Burkardt
//
// Parameters:
//
//
Input/output, int &SEED, a seed for the random number generator.
//
//
Output, double R8_NORMAL_01, a normally distributed random value.
//
{
double r1;
double r2;
const double r8_pi = 3.141592653589793;
double x;
r1 = r8_uniform_01 ( seed );
r2 = r8_uniform_01 ( seed );
x = sqrt ( - 2.0 * log ( r1 ) ) * cos ( 2.0 * r8_pi * r2 );
return x;
}
//****************************************************************************80
double r8_normal_ab ( double a, double b, int &seed )
//****************************************************************************80
//
// Purpose:
//
//
R8_NORMAL_AB returns a scaled pseudonormal R8.
//
// Discussion:
//
//
The normal probability distribution function (PDF) is sampled,
//
with mean A and standard deviation B.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//

// Modified:
//
//
09 April 2012
//
// Author:
//
//
John Burkardt
//
// Parameters:
//
//
Input, double A, the mean of the PDF.
//
//
Input, double B, the standard deviation of the PDF.
//
//
Input/output, int &SEED, a seed for the random number generator.
//
//
Output, double R8_NORMAL_AB, a sample of the normal PDF.
//
{
double value;
value = a + b * r8_normal_01 ( seed );
return value;
}
//****************************************************************************80
double r8_uniform_01 ( int &seed )
//****************************************************************************80
//
// Purpose:
//
//
R8_UNIFORM_01 returns a unit pseudorandom R8.
//
// Discussion:
//
//
This routine implements the recursion
//
//
seed = 16807 * seed mod ( 2^31 - 1 )
//
r8_uniform_01 = seed / ( 2^31 - 1 )
//
//
The integer arithmetic never requires more than 32 bits,
//
including a sign bit.
//
//
If the initial seed is 12345, then the first three computations are
//
//
Input
Output
R8_UNIFORM_01
//
SEED
SEED
//
//
12345 207482415 0.096616
//
207482415 1790989824 0.833995
//
1790989824 2035175616 0.947702
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//

//
09 April 2012
//
// Author:
//
//
John Burkardt
//
// Reference:
//
//
Paul Bratley, Bennett Fox, Linus Schrage,
//
A Guide to Simulation,
//
Springer Verlag, pages 201-202, 1983.
//
//
Pierre L'Ecuyer,
//
Random Number Generation,
//
in Handbook of Simulation
//
edited by Jerry Banks,
//
Wiley Interscience, page 95, 1998.
//
//
Bennett Fox,
//
Algorithm 647:
//
Implementation and Relative Efficiency of Quasirandom
//
Sequence Generators,
//
ACM Transactions on Mathematical Software,
//
Volume 12, Number 4, pages 362-376, 1986.
//
//
Peter Lewis, Allen Goodman, James Miller,
//
A Pseudo-Random Number Generator for the System/360,
//
IBM Systems Journal,
//
Volume 8, pages 136-143, 1969.
//
// Parameters:
//
//
Input/output, int &SEED, the "seed" value. Normally, this
//
value should not be 0. On output, SEED has been updated.
//
//
Output, double R8_UNIFORM_01, a new pseudorandom variate, strictly between
//
0 and 1.
//
{
const int i4_huge = 2147483647;
int k;
double r;
k = seed / 127773;
seed = 16807 * ( seed - k * 127773 ) - k * 2836;
if ( seed < 0 )
{
seed = seed + i4_huge;
}
//
// Although SEED can be represented exactly as a 32 bit integer,
// it generally cannot be represented exactly as a 32 bit real number!
//
r = ( double ) ( seed ) * 4.656612875E-10;
return r;
}
//****************************************************************************80

void r8mat_normal_01 ( int m, int n, int &seed, double x[] )


//****************************************************************************80
//
// Purpose:
//
//
R8MAT_NORMAL_01 returns a unit pseudonormal R8MAT.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
09 April 2012
//
// Author:
//
//
John Burkardt
//
// Reference:
//
//
Paul Bratley, Bennett Fox, Linus Schrage,
//
A Guide to Simulation,
//
Springer Verlag, pages 201-202, 1983.
//
//
Bennett Fox,
//
Algorithm 647:
//
Implementation and Relative Efficiency of Quasirandom
//
Sequence Generators,
//
ACM Transactions on Mathematical Software,
//
Volume 12, Number 4, pages 362-376, 1986.
//
//
Peter Lewis, Allen Goodman, James Miller,
//
A Pseudo-Random Number Generator for the System/360,
//
IBM Systems Journal,
//
Volume 8, pages 136-143, 1969.
//
// Parameters:
//
//
Input, int M, N, the number of rows and columns in the array.
//
//
Input/output, int &SEED, the "seed" value, which should NOT be 0.
//
On output, SEED has been updated.
//
//
Output, double X[M*N], the array of pseudonormal values.
//
{
r8vec_normal_01 ( m*n, seed, x );
return;
}
//****************************************************************************80
double *r8mat_normal_01_new ( int m, int n, int &seed )
//****************************************************************************80
//
// Purpose:

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
{

R8MAT_NORMAL_01_NEW returns a unit pseudonormal R8MAT.


Licensing:
This code is distributed under the GNU LGPL license.
Modified:
09 April 2012
Author:
John Burkardt
Reference:
Paul Bratley, Bennett Fox, Linus Schrage,
A Guide to Simulation,
Springer Verlag, pages 201-202, 1983.
Bennett Fox,
Algorithm 647:
Implementation and Relative Efficiency of Quasirandom
Sequence Generators,
ACM Transactions on Mathematical Software,
Volume 12, Number 4, pages 362-376, 1986.
Peter Lewis, Allen Goodman, James Miller,
A Pseudo-Random Number Generator for the System/360,
IBM Systems Journal,
Volume 8, pages 136-143, 1969.
Parameters:
Input, int M, N, the number of rows and columns in the array.
Input/output, int &SEED, the "seed" value, which should NOT be 0.
On output, SEED has been updated.
Output, double R8MAT_NORMAL_01_NEW[M*N], the array of pseudonormal values.
double *r;
r = r8vec_normal_01_new ( m * n, seed );

return r;
}
//****************************************************************************80
void r8mat_normal_ab ( int m, int n, double a, double b, int &seed, double x[] )
//****************************************************************************80
//
// Purpose:
//
//
R8MAT_NORMAL_AB returns a scaled pseudonormal R8MAT.
//
// Licensing:

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
{

This code is distributed under the GNU LGPL license.


Modified:
09 April 2012
Author:
John Burkardt
Reference:
Paul Bratley, Bennett Fox, Linus Schrage,
A Guide to Simulation,
Springer Verlag, pages 201-202, 1983.
Bennett Fox,
Algorithm 647:
Implementation and Relative Efficiency of Quasirandom
Sequence Generators,
ACM Transactions on Mathematical Software,
Volume 12, Number 4, pages 362-376, 1986.
Peter Lewis, Allen Goodman, James Miller,
A Pseudo-Random Number Generator for the System/360,
IBM Systems Journal,
Volume 8, pages 136-143, 1969.
Parameters:
Input, int M, N, the number of rows and columns in the array.
Input, double A, B, the mean and standard deviation.
Input/output, int &SEED, the "seed" value, which should NOT be 0.
On output, SEED has been updated.
Output, double X[M*N], the array of pseudonormal values.
r8vec_normal_ab ( m * n, a, b, seed, x );

return;
}
//****************************************************************************80
double *r8mat_normal_ab_new ( int m, int n, double a, double b, int &seed )
//****************************************************************************80
//
// Purpose:
//
//
R8MAT_NORMAL_AB_NEW returns a scaled pseudonormal R8MAT.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:

//
//
09 April 2012
//
// Author:
//
//
John Burkardt
//
// Reference:
//
//
Paul Bratley, Bennett Fox, Linus Schrage,
//
A Guide to Simulation,
//
Springer Verlag, pages 201-202, 1983.
//
//
Bennett Fox,
//
Algorithm 647:
//
Implementation and Relative Efficiency of Quasirandom
//
Sequence Generators,
//
ACM Transactions on Mathematical Software,
//
Volume 12, Number 4, pages 362-376, 1986.
//
//
Peter Lewis, Allen Goodman, James Miller,
//
A Pseudo-Random Number Generator for the System/360,
//
IBM Systems Journal,
//
Volume 8, pages 136-143, 1969.
//
// Parameters:
//
//
Input, int M, N, the number of rows and columns in the array.
//
//
Input, double A, B, the mean and standard deviation.
//
//
Input/output, int &SEED, the "seed" value, which should NOT be 0.
//
On output, SEED has been updated.
//
//
Output, double R8MAT_NORMAL_AB_NEW[M*N], the array of pseudonormal values.
//
{
double *r;
r = r8vec_normal_ab_new ( m * n, a, b, seed );
return r;
}
//****************************************************************************80
void r8mat_print ( int m, int n, double a[], string title )
//****************************************************************************80
//
// Purpose:
//
//
R8MAT_PRINT prints an R8MAT.
//
// Discussion:
//
//
An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
//
in column-major order.
//
//
Entry A(I,J) is stored as A[I+J*M]
//

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
{

Licensing:
This code is distributed under the GNU LGPL license.
Modified:
10 September 2009
Author:
John Burkardt
Parameters:
Input, int M, the number of rows in A.
Input, int N, the number of columns in A.
Input, double A[M*N], the M by N matrix.
Input, string TITLE, a title.
r8mat_print_some ( m, n, a, 1, 1, m, n, title );

return;
}
//****************************************************************************80
void r8mat_print_some ( int m, int n, double a[], int ilo, int jlo, int ihi,
int jhi, string title )
//****************************************************************************80
//
// Purpose:
//
//
R8MAT_PRINT_SOME prints some of an R8MAT.
//
// Discussion:
//
//
An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
//
in column-major order.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
26 June 2013
//
// Author:
//
//
John Burkardt
//
// Parameters:
//
//
Input, int M, the number of rows of the matrix.
//
M must be positive.
//

//
Input, int N, the number of columns of the matrix.
//
N must be positive.
//
//
Input, double A[M*N], the matrix.
//
//
Input, int ILO, JLO, IHI, JHI, designate the first row and
//
column, and the last row and column to be printed.
//
//
Input, string TITLE, a title.
//
{
# define INCX 5
int
int
int
int
int
int

i;
i2hi;
i2lo;
j;
j2hi;
j2lo;

cout << "\n";


cout << title << "\n";
if ( m <= 0 || n <= 0 )
{
cout << "\n";
cout << " (None)\n";
return;
}
//
// Print the columns of the matrix, in strips of 5.
//
for ( j2lo = jlo; j2lo <= jhi; j2lo = j2lo + INCX )
{
j2hi = j2lo + INCX - 1;
if ( n < j2hi )
{
j2hi = n;
}
if ( jhi < j2hi )
{
j2hi = jhi;
}
cout << "\n";
//
// For each column J in the current range...
//
// Write the header.
//
cout << " Col:
";
for ( j = j2lo; j <= j2hi; j++ )
{
cout << setw(7) << j - 1 << "
";
}
cout << "\n";
cout << " Row\n";
cout << "\n";
//
// Determine the range of the rows in this strip.
//

if ( 1 <
{
i2lo =
}
else
{
i2lo =
}
if ( ihi
{
i2hi =
}
else
{
i2hi =
}

ilo )
ilo;

1;
< m )
ihi;

m;

for ( i = i2lo; i <= i2hi; i++ )


{
//
// Print out (up to) 5 entries in row I, that lie in the current strip.
//
cout << setw(5) << i - 1 << ": ";
for ( j = j2lo; j <= j2hi; j++ )
{
cout << setw(12) << a[i-1+(j-1)*m] << " ";
}
cout << "\n";
}
}
return;
# undef INCX
}
//****************************************************************************80
void r8vec_normal_01 ( int n, int &seed, double x[] )
//****************************************************************************80
//
// Purpose:
//
//
R8VEC_NORMAL_01 returns a unit pseudonormal R8VEC.
//
// Discussion:
//
//
The standard normal probability distribution function (PDF) has
//
mean 0 and standard deviation 1.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
06 August 2013
//
// Author:
//
//
John Burkardt

//
// Parameters:
//
//
Input, int N, the number of values desired.
//
//
Input/output, int &SEED, a seed for the random number generator.
//
//
Output, double X[N], a sample of the standard normal PDF.
//
// Local parameters:
//
//
Local, double R(N+1), is used to store some uniform random values.
//
Its dimension is N+1, but really it is only needed to be the
//
smallest even number greater than or equal to N.
//
//
Local, int X_LO, X_HI, records the range of entries of
//
X that we need to compute
//
{
int i;
int m;
double *r;
const double r8_pi = 3.141592653589793;
int x_hi;
int x_lo;
//
// Record the range of X we need to fill in.
//
x_lo = 1;
x_hi = n;
//
// If we need just one new value, do that here to avoid null arrays.
//
if ( x_hi - x_lo + 1 == 1 )
{
r = r8vec_uniform_01_new ( 2, seed );
x[x_hi-1] = sqrt ( - 2.0 * log ( r[0] ) ) * cos ( 2.0 * r8_pi * r[1] );
delete [] r;
}
//
// If we require an even number of values, that's easy.
//
else if ( ( x_hi - x_lo + 1 ) % 2 == 0 )
{
m = ( x_hi - x_lo + 1 ) / 2;
r = r8vec_uniform_01_new ( 2*m, seed );
for ( i = 0; i <= 2 * m - 2; i = i + 2 )
{
x[x_lo+i-1] = sqrt ( - 2.0 * log ( r[i] ) ) * cos ( 2.0 * r8_pi * r[i+1] )
;
x[x_lo+i ] = sqrt ( - 2.0 * log ( r[i] ) ) * sin ( 2.0 * r8_pi * r[i+1] )
;
}
delete [] r;
}

//
// If we require an odd number of values, we generate an even number,
// and handle the last pair specially, storing one in X(N).
//
else
{
x_hi = x_hi - 1;
m = ( x_hi - x_lo + 1 ) / 2 + 1;
r = r8vec_uniform_01_new ( 2*m, seed );
for ( i = 0; i <= 2 * m - 4; i = i + 2 )
{
x[x_lo+i-1] = sqrt ( - 2.0 * log ( r[i] ) ) * cos ( 2.0 * r8_pi * r[i+1] )
;
x[x_lo+i ] = sqrt ( - 2.0 * log ( r[i] ) ) * sin ( 2.0 * r8_pi * r[i+1] )
;
}
i = 2*m - 2;
x[x_lo+i-1] = sqrt ( - 2.0 * log ( r[i] ) ) * cos ( 2.0 * r8_pi * r[i+1] );
delete [] r;
}
return;
}
//****************************************************************************80
double *r8vec_normal_01_new ( int n, int &seed )
//****************************************************************************80
//
// Purpose:
//
//
R8VEC_NORMAL_01_NEW returns a unit pseudonormal R8VEC.
//
// Discussion:
//
//
The standard normal probability distribution function (PDF) has
//
mean 0 and standard deviation 1.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
06 August 2013
//
// Author:
//
//
John Burkardt
//
// Parameters:
//
//
Input, int N, the number of values desired.
//

//
Input/output, int &SEED, a seed for the random number generator.
//
//
Output, double R8VEC_NORMAL_01_NEW[N], a sample of the standard normal PDF
.
//
// Local parameters:
//
//
Local, double R(N+1), is used to store some uniform random values.
//
Its dimension is N+1, but really it is only needed to be the
//
smallest even number greater than or equal to N.
//
//
Local, int X_LO, X_HI, records the range of entries of
//
X that we need to compute.
//
{
int i;
int m;
double *r;
const double r8_pi = 3.141592653589793;
double *x;
int x_hi;
int x_lo;
x = new double[n];
//
// Record the range of X we need to fill in.
//
x_lo = 1;
x_hi = n;
//
// If we need just one new value, do that here to avoid null arrays.
//
if ( x_hi - x_lo + 1 == 1 )
{
r = r8vec_uniform_01_new ( 2, seed );
x[x_hi-1] = sqrt ( - 2.0 * log ( r[0] ) ) * cos ( 2.0 * r8_pi * r[1] );
delete [] r;
}
//
// If we require an even number of values, that's easy.
//
else if ( ( x_hi - x_lo + 1 ) % 2 == 0 )
{
m = ( x_hi - x_lo + 1 ) / 2;
r = r8vec_uniform_01_new ( 2*m, seed );
for ( i = 0; i <= 2 * m - 2; i = i + 2 )
{
x[x_lo+i-1] = sqrt ( - 2.0 * log ( r[i] ) ) * cos ( 2.0 * r8_pi * r[i+1] )
;
x[x_lo+i ] = sqrt ( - 2.0 * log ( r[i] ) ) * sin ( 2.0 * r8_pi * r[i+1] )
;
}
delete [] r;
}
//

// If we require an odd number of values, we generate an even number,


// and handle the last pair specially, storing one in X(N), and
// saving the other for later.
//
else
{
x_hi = x_hi - 1;
m = ( x_hi - x_lo + 1 ) / 2 + 1;
r = r8vec_uniform_01_new ( 2*m, seed );
for ( i = 0; i <= 2 * m - 4; i = i + 2 )
{
x[x_lo+i-1] = sqrt ( - 2.0 * log ( r[i] ) ) * cos ( 2.0 * r8_pi * r[i+1] )
;
x[x_lo+i ] = sqrt ( - 2.0 * log ( r[i] ) ) * sin ( 2.0 * r8_pi * r[i+1] )
;
}
i = 2*m - 2;
x[x_lo+i-1] = sqrt ( - 2.0 * log ( r[i] ) ) * cos ( 2.0 * r8_pi * r[i+1] );
delete [] r;
}
return x;
}
//****************************************************************************80
void r8vec_normal_ab ( int n, double b, double c, int &seed, double x[] )
//****************************************************************************80
//
// Purpose:
//
//
R8VEC_NORMAL_AB returns a scaled pseudonormal R8VEC.
//
// Discussion:
//
//
The scaled normal probability distribution function (PDF) has
//
mean A and standard deviation B.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
06 August 2013
//
// Author:
//
//
John Burkardt
//
// Parameters:
//
//
Input, int N, the number of values desired.
//

//
Input, double B, C, the mean and standard deviation.
//
//
Input/output, int &SEED, a seed for the random number generator.
//
//
Output, double X[N], a sample of the standard normal PDF.
//
// Local parameters:
//
//
Local, double R(N+1), is used to store some uniform random values.
//
Its dimension is N+1, but really it is only needed to be the
//
smallest even number greater than or equal to N.
//
//
Local, int X_LO, X_HI, records the range of entries of
//
X that we need to compute.
//
{
int i;
int m;
double *r;
const double r8_pi = 3.141592653589793;
int x_hi;
int x_lo;
x = new double[n];
//
// Record the range of X we need to fill in.
//
x_lo = 1;
x_hi = n;
//
// If we need just one new value, do that here to avoid null arrays.
//
if ( x_hi - x_lo + 1 == 1 )
{
r = r8vec_uniform_01_new ( 2, seed );
x[x_hi-1] = sqrt ( - 2.0 * log ( r[0] ) ) * cos ( 2.0 * r8_pi * r[1] );
delete [] r;
}
//
// If we require an even number of values, that's easy.
//
else if ( ( x_hi - x_lo + 1 ) % 2 == 0 )
{
m = ( x_hi - x_lo + 1 ) / 2;
r = r8vec_uniform_01_new ( 2*m, seed );
for ( i = 0; i <= 2 * m - 2; i = i + 2 )
{
x[x_lo+i-1] = sqrt ( - 2.0 * log ( r[i] ) ) * cos ( 2.0 * r8_pi * r[i+1] )
;
x[x_lo+i ] = sqrt ( - 2.0 * log ( r[i] ) ) * sin ( 2.0 * r8_pi * r[i+1] )
;
}
delete [] r;
}
//

// If we require an odd number of values, we generate an even number,


// and handle the last pair specially, storing one in X(N).
//
else
{
x_hi = x_hi - 1;
m = ( x_hi - x_lo + 1 ) / 2 + 1;
r = r8vec_uniform_01_new ( 2*m, seed );
for ( i = 0; i <= 2 * m - 4; i = i + 2 )
{
x[x_lo+i-1] = sqrt ( - 2.0 * log ( r[i] ) ) * cos ( 2.0 * r8_pi * r[i+1] )
;
x[x_lo+i ] = sqrt ( - 2.0 * log ( r[i] ) ) * sin ( 2.0 * r8_pi * r[i+1] )
;
}
i = 2*m - 2;
x[x_lo+i-1] = sqrt ( - 2.0 * log ( r[i] ) ) * cos ( 2.0 * r8_pi * r[i+1] );
delete [] r;
}
for ( i = 0; i < n; i++ )
{
x[i] = b + c * x[i];
}
return;
}
//****************************************************************************80
double *r8vec_normal_ab_new ( int n, double b, double c, int &seed )
//****************************************************************************80
//
// Purpose:
//
//
R8VEC_NORMAL_AB_NEW returns a scaled pseudonormal R8VEC.
//
// Discussion:
//
//
The scaled normal probability distribution function (PDF) has
//
mean A and standard deviation B.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
06 August 2013
//
// Author:
//
//
John Burkardt
//

// Parameters:
//
//
Input, int N, the number of values desired.
//
//
Input, double B, C, the mean and standard deviation.
//
//
Input/output, int &SEED, a seed for the random number generator.
//
//
Output, double R8VEC_NORMAL_AB_NEW[N], a sample of the standard normal PDF
.
//
// Local parameters:
//
//
Local, double R(N+1), is used to store some uniform random values.
//
Its dimension is N+1, but really it is only needed to be the
//
smallest even number greater than or equal to N.
//
//
Local, int X_LO, X_HI, records the range of entries of
//
X that we need to compute.
//
{
int i;
int m;
double *r;
const double r8_pi = 3.141592653589793;
double *x;
int x_hi;
int x_lo;
x = new double[n];
//
// Record the range of X we need to fill in.
//
x_lo = 1;
x_hi = n;
//
// If we need just one new value, do that here to avoid null arrays.
//
if ( x_hi - x_lo + 1 == 1 )
{
r = r8vec_uniform_01_new ( 2, seed );
x[x_hi-1] = sqrt ( - 2.0 * log ( r[0] ) ) * cos ( 2.0 * r8_pi * r[1] );
delete [] r;
}
//
// If we require an even number of values, that's easy.
//
else if ( ( x_hi - x_lo + 1 ) % 2 == 0 )
{
m = ( x_hi - x_lo + 1 ) / 2;
r = r8vec_uniform_01_new ( 2*m, seed );
for ( i = 0; i <= 2 * m - 2; i = i + 2 )
{
x[x_lo+i-1] = sqrt ( - 2.0 * log ( r[i] ) ) * cos ( 2.0 * r8_pi * r[i+1] )
;
x[x_lo+i ] = sqrt ( - 2.0 * log ( r[i] ) ) * sin ( 2.0 * r8_pi * r[i+1] )

;
}
delete [] r;
}
//
// If we require an odd number of values, we generate an even number,
// and handle the last pair specially.
//
else
{
x_hi = x_hi - 1;
m = ( x_hi - x_lo + 1 ) / 2 + 1;
r = r8vec_uniform_01_new ( 2 * m, seed );
for ( i = 0; i <= 2 * m - 4; i = i + 2 )
{
x[x_lo+i-1] = sqrt ( - 2.0 * log ( r[i] ) ) * cos ( 2.0 * r8_pi * r[i+1] )
;
x[x_lo+i ] = sqrt ( - 2.0 * log ( r[i] ) ) * sin ( 2.0 * r8_pi * r[i+1] )
;
}
i = 2*m - 2;
x[x_lo+i-1] = sqrt ( - 2.0 * log ( r[i] ) ) * cos ( 2.0 * r8_pi * r[i+1] );
delete [] r;
}
for ( i = 0; i < n; i++ )
{
x[i] = b + c * x[i];
}
return x;
}
//****************************************************************************80
void r8vec_print ( int n, double a[], string title )
//****************************************************************************80
//
// Purpose:
//
//
R8VEC_PRINT prints an R8VEC.
//
// Discussion:
//
//
An R8VEC is a vector of R8's.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
16 August 2004

//
// Author:
//
//
John Burkardt
//
// Parameters:
//
//
Input, int N, the number of components of the vector.
//
//
Input, double A[N], the vector to be printed.
//
//
Input, string TITLE, a title.
//
{
int i;
cout << "\n";
cout << title << "\n";
cout << "\n";
for ( i = 0; i < n; i++ )
{
cout << " " << setw(8) << i
<< ": " << setw(14) << a[i] << "\n";
}
return;
}
//****************************************************************************80
double *r8vec_uniform_01_new ( int n, int &seed )
//****************************************************************************80
//
// Purpose:
//
//
R8VEC_UNIFORM_01_NEW returns a new unit pseudorandom R8VEC.
//
// Discussion:
//
//
This routine implements the recursion
//
//
seed = ( 16807 * seed ) mod ( 2^31 - 1 )
//
u = seed / ( 2^31 - 1 )
//
//
The integer arithmetic never requires more than 32 bits,
//
including a sign bit.
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
09 April 2012
//
// Author:
//
//
John Burkardt
//
// Reference:

//
//
Paul Bratley, Bennett Fox, Linus Schrage,
//
A Guide to Simulation,
//
Second Edition,
//
Springer, 1987,
//
ISBN: 0387964673,
//
LC: QA76.9.C65.B73.
//
//
Bennett Fox,
//
Algorithm 647:
//
Implementation and Relative Efficiency of Quasirandom
//
Sequence Generators,
//
ACM Transactions on Mathematical Software,
//
Volume 12, Number 4, December 1986, pages 362-376.
//
//
Pierre L'Ecuyer,
//
Random Number Generation,
//
in Handbook of Simulation,
//
edited by Jerry Banks,
//
Wiley, 1998,
//
ISBN: 0471134031,
//
LC: T57.62.H37.
//
//
Peter Lewis, Allen Goodman, James Miller,
//
A Pseudo-Random Number Generator for the System/360,
//
IBM Systems Journal,
//
Volume 8, Number 2, 1969, pages 136-143.
//
// Parameters:
//
//
Input, int N, the number of entries in the vector.
//
//
Input/output, int &SEED, a seed for the random number generator.
//
//
Output, double R8VEC_UNIFORM_01_NEW[N], the vector of pseudorandom values.
//
{
int i;
int i4_huge = 2147483647;
int k;
double *r;
if ( seed == 0 )
{
cerr << "\n";
cerr << "R8VEC_UNIFORM_01_NEW - Fatal error!\n";
cerr << " Input value of SEED = 0.\n";
exit ( 1 );
}
r = new double[n];
for ( i = 0; i < n; i++ )
{
k = seed / 127773;
seed = 16807 * ( seed - k * 127773 ) - k * 2836;
if ( seed < 0 )
{

seed = seed + i4_huge;


}
r[i] = ( double ) ( seed ) * 4.656612875E-10;
}
return r;
}
//****************************************************************************80
void timestamp ( )
//****************************************************************************80
//
// Purpose:
//
//
TIMESTAMP prints the current YMDHMS date as a time stamp.
//
// Example:
//
//
31 May 2001 09:45:54 AM
//
// Licensing:
//
//
This code is distributed under the GNU LGPL license.
//
// Modified:
//
//
08 July 2009
//
// Author:
//
//
John Burkardt
//
// Parameters:
//
//
None
//
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct std::tm *tm_ptr;
size_t len;
std::time_t now;
now = std::time ( NULL );
tm_ptr = std::localtime ( &now );
len = std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr )
;
std::cout << time_buffer << "\n";
return;
# undef TIME_SIZE
}

You might also like