Random numbers

TO SUPPORT MY WORK, ORDER A COMMERCIAL LICENSE
THANK YOU!

Tutorial Math Collection API spec Download

Below is the code for JAVA, the code for C# is almost identical.

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.5.0.0] Res. 1: rUni(1,2) = 1.9863589681879383
[mXparser-v.5.0.0] Res. 2: rUni(1,2) = 1.4058795822306704

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.5.0.0] Res. 1: rUnid(1,10) = 1.0
[mXparser-v.5.0.0] Res. 2: rUnid(1,10) = 5.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.5.0.0] Res. 1: rNor(0,1) = 1.9129882228979274
[mXparser-v.5.0.0] Res. 2: rNor(0,1) = -0.7067519384541234

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.5.0.0] Res. 1: rList(0,3,6,9,12) = 0.0
[mXparser-v.5.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.5.0.0] Res.   10: avg(i, 1,   10, rNor(2,4) ) = 2.4328947641208645
[mXparser-v.5.0.0] Res.  100: avg(i, 1,  100, rNor(2,4) ) = 1.6711629865899573
[mXparser-v.5.0.0] Res. 1000: avg(i, 1, 1000, rNor(2,4) ) = 1.8369376572327

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.5.0.0] Res.   10: stdi(i, 1,   10, rNor(2,4) ) = 3.954874087003795
[mXparser-v.5.0.0] Res.  100: stdi(i, 1,  100, rNor(2,4) ) = 4.080722774419512
[mXparser-v.5.0.0] Res. 1000: stdi(i, 1, 1000, rNor(2,4) ) = 3.9901541513377463

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.5.0.0] Res.   10: vari(i, 1,   10, rNor(2,4) ) = 9.818662394020286
[mXparser-v.5.0.0] Res.  100: vari(i, 1,  100, rNor(2,4) ) = 14.949344325002938
[mXparser-v.5.0.0] Res. 1000: vari(i, 1, 1000, rNor(2,4) ) = 15.365725940366103
Nuget
Install-Package MathParser.org-mXparser -Version 5.0.2
Maven
<dependency>
<groupid>org.mariuszgromada.math</groupid>
<artifactid>MathParser.org-mXparser</artifactid>
<version>5.0.2</version>
</dependency>
Gradle
implementation 'org.mariuszgromada.math:MathParser.org-mXparser:5.0.2'
Gradle (Kotlin)
implementation("org.mariuszgromada.math:MathParser.org-mXparser:5.0.2")
GitHub
git clone https://github.com/mariuszgromada/MathParser.org-mXparser
OTHER DOWNLOAD OPTIONS

Download latest release – v.5.0.2 Leonis: bin + docDownload latest release – v.5.0.2 Leonis: bin only, includes separate binaries for various .NET platforms and Java versions

NEWS FROM MATHPARSER.ORG
SOURCE CODE

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

DONATION
Did you find the software useful?
Please consider donation 🙂
DONATE