I am given the following problem to solve (This text is translated from Russian. So, there may be some translation issues):
... Another method to draw from the normal distribution is to draw two independent random numbers from the uniform distribution x1, x2 ∈ [0:0, 1:0). Then apply the following transformation:
resulting in two randomly independent numbers n1, n2 from a normal distribution with zero expected value and unit variance.To change the distribution parameters to other parameters, e.g. the expected value for and the variance to, you should multiply the result of the draw by and add, i.e.
In the equation above, N(μ, σ) is a random variable with normal distribution with expected value μ and variance σ.According to the Maxwell distribution, each component (x, y or z) of the velocity vector v is a random variable from a normal distribution with zero expected value, and variance
where m is the mass of the molecule, T is the temperature in Kelvin, kB is Boltzmann constant.Your task: Draw 10,000 velocity vectors for the N2 nitrogen molecule at 300K. Calculate the average length of these vectors, and therefore the average value of the speed of the nitrogen molecule, using the formula:
![]()
public class Maxwell
{
public double N1 { get; private set; }
public double N2 { get; private set; }
public void Compute(Random random)
{
double x1 = random.NextDouble();
double x2 = random.NextDouble();
N1 = Math.Sqrt(-2 * Math.Log(x1)) * Math.Cos(2 * Math.PI * x2);
N2 = Math.Sqrt(-2 * Math.Log(x1)) * Math.Sin(2 * Math.PI * x2);
}
}
public class Program
{
static void Main(string[] args)
{
Random r = new Random();
Maxwell m = new Maxwell();
m.Compute(r);
double n1 = m.N1;
double n2 = m.N2;
//.....?
}
}
I am not understanding how to implement N(μ, σ) from n1 and n2, and how to get to vector v from there.
Can anyone help?
Edit: I have implemented it on the basis of Eric Lippert's answer:
using System;
public class CommonDistributions
{
public static double Uniform(Random random)
{
return random.NextDouble();
}
static double Gaussian(Random random)
{
return Math.Sqrt(-2 * Math.Log(Uniform(random))) * Math.Cos(2 * Math.PI * Uniform(random));
}
public static double Gaussian(Random random, double mu, double sigma)
{
return sigma * Gaussian(random) + mu;
}
}
public class MaxwellBolzman
{
static double KB = 1.38064852e-23;
static double MaxwellVariance(double mass, double temperature)
{
return Math.Sqrt(KB * temperature / mass);
}
static double MaxwellComponent(Random random, double mass, double temperature)
{
double mu = 0.0;
double sigma = MaxwellVariance(mass, temperature);
return CommonDistributions.Gaussian(random, mu, sigma);
}
public static double Maxwell(Random random, double mass, double temperature)
{
double one = MaxwellComponent(random, mass, temperature);
double two = MaxwellComponent(random, mass, temperature);
double thr = MaxwellComponent(random, mass, temperature);
return Math.Sqrt(one * one + two * two + thr * thr);
}
}
public static class MainClass
{
public static void Main(String[] args)
{
Random random = new Random();
const int N = 10000;
const int T = 300;//300K
const double mass = 28.02;//28.02 g/mol
double sum = 0.0;
for (int i = 1; i < N; i++)
{
sum = sum + MaxwellBolzman.Maxwell(random, mass, T);
}
Console.WriteLine($"Maxwell-Boltzman = {sum/N}");
string str = string.Empty;
}
}
I am not sure about the values of temperature and the mass of Nitrogen 2.
It would be nice if someone can comment on the code.
Random
. Not sure whatstr
is doing at the end there though.str
is for setting a debugging point.}
, or you can make a statement that is a single;
and break on that. Or, a common choice in this sort of program is to end it withConsole.WriteLine("Press enter to exit"); Console.ReadLine();
, so that your program always pauses before it exits and you have a place to put the breakpoint.