Random numbers

MathParser.org - mXparser - logo - Tutorial

*** If you found the software useful donation is something you might consider 🙂 ***

If you found the software useful donation is something you might consider :-)

JAVA code

Case 1: Random number from uniform continuous distribution

X\sim U(1,2)

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.3.0.0] Res. 1: rUni(1,2) = 1.8106318179376182
[mXparser-v.3.0.0] Res. 2: rUni(1,2) = 1.9094469937672058

Case 2: Random number from uniform discrete distribution

X\sim U\{1,10\}

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.3.0.0] Res. 1: rUnid(1,10) = 10.0
[mXparser-v.3.0.0] Res. 2: rUnid(1,10) = 7.0

Case 3: Random number from normal distribution

X\sim N(0,1)

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.3.0.0] Res. 1: rNor(0,1) = -0.494157964626926
[mXparser-v.3.0.0] Res. 2: rNor(0,1) = 2.138300367979923

Case 4: Random number from a given list

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

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.3.0.0] Res. 1: rList(0,3,6,9,12) = 12.0
[mXparser-v.3.0.0] Res. 2: rList(0,3,6,9,12) = 3.0

Case 5: Estimating mean of Normal distribution

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

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.3.0.0] Res.   10: avg(i, 1,   10, rNor(2,4) ) = 1.742530625748534
[mXparser-v.3.0.0] Res.  100: avg(i, 1,  100, rNor(2,4) ) = 2.493566953138476
[mXparser-v.3.0.0] Res. 1000: avg(i, 1, 1000, rNor(2,4) ) = 2.044750479834757

Case 6: Estimating standard deviation of Normal distribution

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

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.3.0.0] Res.   10: stdi(i, 1,   10, rNor(2,4) ) = 4.497319834825644
[mXparser-v.3.0.0] Res.  100: stdi(i, 1,  100, rNor(2,4) ) = 3.728670812065406
[mXparser-v.3.0.0] Res. 1000: stdi(i, 1, 1000, rNor(2,4) ) = 3.940285556971465

Case 7: Estimating variance of Normal distribution

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

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.3.0.0] Res.   10: vari(i, 1,   10, rNor(2,4) ) = 7.598316602747062
[mXparser-v.3.0.0] Res.  100: vari(i, 1,  100, rNor(2,4) ) = 18.39442617100682
[mXparser-v.3.0.0] Res. 1000: vari(i, 1, 1000, rNor(2,4) ) = 16.08545019918789

*** If you found the software useful donation is something you might consider 🙂 ***

If you found the software useful donation is something you might consider :-)

Enjoy! 🙂

 

Best regards,

Mariusz Gromada

Download latest release – v.4.1.1 Aeries: bin + doc + src (.zip 13.4 MB)

Download latest release – v.4.1.1 Aeries: bin only (.zip 4.35 MB) – separate binaries for java: 1.5, 1.6, 1.7, 1.8; .NET: 2.0, 3.0, 3.5, 4.0, 4.5, 4.6, 4.7, .NET Core, .NET Standard, .NET PCL, Xamarin.Android, Xamarin.iOS

Source code .zipSource code .tar.gz View on GitHubMathSpace.pl