Random numbers

Case 1: Random number from uniform continuous distribution

$$X\sim U(1,2)$$

// JAVA: import org.mariuszgromada.math.mxparser.*;
// ...
Expression e = new Expression("rUni(1,2)");

mXparser.consolePrintln("Res. 1: " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Res. 2: " + e.getExpressionString() + " = " + e.calculate());

[mXparser-v.5.2.1] Res. 1: rUni(1,2) = 1.5888498050890085
[mXparser-v.5.2.1] Res. 2: rUni(1,2) = 1.1924712083282079


Case 2: Random number from uniform discrete distribution

$$X\sim U\{1,10\}$$

// JAVA: import org.mariuszgromada.math.mxparser.*;
// ...
Expression e = new Expression("rUnid(1,10)");

mXparser.consolePrintln("Res. 1: " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Res. 2: " + e.getExpressionString() + " = " + e.calculate());

[mXparser-v.5.2.1] Res. 1: rUnid(1,10) = 2.0
[mXparser-v.5.2.1] Res. 2: rUnid(1,10) = 5.0


Case 3: Random number from normal distribution

$$X\sim N(0,1)$$

// JAVA: import org.mariuszgromada.math.mxparser.*;
// ...
Expression e = new Expression("rNor(0,1)");

mXparser.consolePrintln("Res. 1: " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Res. 2: " + e.getExpressionString() + " = " + e.calculate());

[mXparser-v.5.2.1] Res. 1: rNor(0,1) = -0.30478909669429705
[mXparser-v.5.2.1] Res. 2: rNor(0,1) = -0.5954641115740769


Case 4: Random number from a given list

$$X\sim \{0,3,6,9,12\}$$

// JAVA: import org.mariuszgromada.math.mxparser.*;
// ...
Expression e = new Expression("rList(0,3,6,9,12)");

mXparser.consolePrintln("Res. 1: " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Res. 2: " + e.getExpressionString() + " = " + e.calculate());

[mXparser-v.5.2.1] Res. 1: rList(0,3,6,9,12) = 0.0
[mXparser-v.5.2.1] Res. 2: rList(0,3,6,9,12) = 9.0


Case 5: Estimating mean of Normal distribution

$$N(\mu,\sigma) \quad \mu=2, \quad \sigma = 4, \quad \sigma^2 = 16$$

// JAVA: import org.mariuszgromada.math.mxparser.*;
// ...
Expression e10   = new Expression("avg(i, 1,   10, rNor(2,4) )");
Expression e100  = new Expression("avg(i, 1,  100, rNor(2,4) )");
Expression e1000 = new Expression("avg(i, 1, 1000, rNor(2,4) )");

mXparser.consolePrintln("Res.   10: " + e10.getExpressionString() + " = " + e10.calculate());
mXparser.consolePrintln("Res.  100: " + e100.getExpressionString() + " = " + e100.calculate());
mXparser.consolePrintln("Res. 1000: " + e1000.getExpressionString() + " = " + e1000.calculate());

[mXparser-v.5.2.1] Res.   10: avg(i, 1,   10, rNor(2,4) ) = 2.1522214658659204
[mXparser-v.5.2.1] Res.  100: avg(i, 1,  100, rNor(2,4) ) = 2.3579992620417602
[mXparser-v.5.2.1] Res. 1000: avg(i, 1, 1000, rNor(2,4) ) = 1.8003777705366013


Case 6: Estimating standard deviation of Normal distribution

$$N(\mu,\sigma) \quad \mu=2, \quad \sigma = 4, \quad \sigma^2 = 16$$

// JAVA: import org.mariuszgromada.math.mxparser.*;
// ...
Expression e10   = new Expression("stdi(i, 1,   10, rNor(2,4) )");
Expression e100  = new Expression("stdi(i, 1,  100, rNor(2,4) )");
Expression e1000 = new Expression("stdi(i, 1, 1000, rNor(2,4) )");

mXparser.consolePrintln("Res.   10: " + e10.getExpressionString() + " = " + e10.calculate());
mXparser.consolePrintln("Res.  100: " + e100.getExpressionString() + " = " + e100.calculate());
mXparser.consolePrintln("Res. 1000: " + e1000.getExpressionString() + " = " + e1000.calculate());

[mXparser-v.5.2.1] Res.   10: stdi(i, 1,   10, rNor(2,4) ) = 3.2972707623194517
[mXparser-v.5.2.1] Res.  100: stdi(i, 1,  100, rNor(2,4) ) = 3.662315094862741
[mXparser-v.5.2.1] Res. 1000: stdi(i, 1, 1000, rNor(2,4) ) = 3.888248902401271


Case 7: Estimating variance of Normal distribution

$$N(\mu,\sigma) \quad \mu=2, \quad \sigma = 4, \quad \sigma^2 = 16$$

// JAVA: import org.mariuszgromada.math.mxparser.*;
// ...
Expression e10   = new Expression("vari(i, 1,   10, rNor(2,4) )");
Expression e100  = new Expression("vari(i, 1,  100, rNor(2,4) )");
Expression e1000 = new Expression("vari(i, 1, 1000, rNor(2,4) )");

mXparser.consolePrintln("Res.   10: " + e10.getExpressionString() + " = " + e10.calculate());
mXparser.consolePrintln("Res.  100: " + e100.getExpressionString() + " = " + e100.calculate());
mXparser.consolePrintln("Res. 1000: " + e1000.getExpressionString() + " = " + e1000.calculate());

[mXparser-v.5.2.1] Res.   10: vari(i, 1,   10, rNor(2,4) ) = 29.270037694704158
[mXparser-v.5.2.1] Res.  100: vari(i, 1,  100, rNor(2,4) ) = 19.919087917569303
[mXparser-v.5.2.1] Res. 1000: vari(i, 1, 1000, rNor(2,4) ) = 17.27420650241558

Nuget – Package Manager

Install-Package MathParser.org-mXparser -Version 6.0.0

Nuget – .NET CLI

dotnet add package MathParser.org-mXparser --version 6.0.0

Nuget – Package Reference

<PackageReference Include="MathParser.org-mXparser" Version="6.0.0"/>

Maven – Dependency

<dependency><groupid>org.mariuszgromada.math</groupid><artifactid>MathParser.org-mXparser</artifactid><version>6.0.0</version></dependency>

implementation 'org.mariuszgromada.math:MathParser.org-mXparser:6.0.0'

implementation("org.mariuszgromada.math:MathParser.org-mXparser:6.0.0")

GitHub

git clone https://github.com/mariuszgromada/MathParser.org-mXparser