Random numbers

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

The tutorial consists of more than 200 live examples from 50 sections given separately for JAVA, C# and C++. Each of the examples can be copied and run on your own environment. In addition, mXparser provides an extensive collection of over 500 built-in math functions, expressions and symbols. Familiarize yourself with the scope and the syntax. Live testing is the best way to learn. Good luck! 🙂

Tutorial Math Collection API spec Download

Below is the code for JAVA, C# (the code for C# is almost identical) and C++. To copy the code, double-click inside the frame.

Case 1: Random number from uniform continuous distribution

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

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("rUni(1,2)");

mXparser::consolePrintln("Res. 1: " + e->getExpressionString() + " = " + e->calculate());
mXparser::consolePrintln("Res. 2: " + e->getExpressionString() + " = " + e->calculate());
Code result
[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/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("rUnid(1,10)");

mXparser::consolePrintln("Res. 1: " + e->getExpressionString() + " = " + e->calculate());
mXparser::consolePrintln("Res. 2: " + e->getExpressionString() + " = " + e->calculate());
Code result
[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/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("rNor(0,1)");

mXparser::consolePrintln("Res. 1: " + e->getExpressionString() + " = " + e->calculate());
mXparser::consolePrintln("Res. 2: " + e->getExpressionString() + " = " + e->calculate());
Code result
[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/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr 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());
Code result
[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/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e10   = new_Expression("avg(i, 1,   10, rNor(2,4) )");
ExpressionPtr e100  = new_Expression("avg(i, 1,  100, rNor(2,4) )");
ExpressionPtr 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());
Code result
[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/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e10   = new_Expression("stdi(i, 1,   10, rNor(2,4) )");
ExpressionPtr e100  = new_Expression("stdi(i, 1,  100, rNor(2,4) )");
ExpressionPtr 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());
Code result
[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/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e10   = new_Expression("vari(i, 1,   10, rNor(2,4) )");
ExpressionPtr e100  = new_Expression("vari(i, 1,  100, rNor(2,4) )");
ExpressionPtr 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());
Code result
[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 (C#, F#, Visual Basic, …)

Install-Package MathParser.org-mXparser -Version 6.1.0

Nuget – .NET CLI

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

Nuget – Package Reference

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

Maven – Dependency (Java, Kotlin, Scala, Groovy, …)

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

Maven – Gradle

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

CMake – Dependency / FetchContent (C++, MSVC, LLVM/Clang, GNU/GCC, MinGW, MSYS2, WSL, Windows, Linux, Unix, MacOS)

include(FetchContent)
FetchContent_Declare(
MathParserOrgMxParser
GIT_REPOSITORY
https://github.com/mariuszgromada/MathParser.org-mXparser.git
GIT_TAG
v.6.1.0
SOURCE_SUBDIR CURRENT/cpp/lib
)
FetchContent_MakeAvailable(
MathParserOrgMxParser)
target_link_libraries(YourExecutable
MathParserOrgMxParser)

GitHub

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

OTHER DOWNLOAD OPTIONS

Download latest release – v.6.1.0 Sagitara: .NET bin onlyDownload latest release – v.6.1.0 Sagitara: JAVA bin onlyDownload latest release – v.6.1.0 Sagitara: bin + doc

NEWS FROM MATHPARSER.ORG
SOURCE CODE

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

My other creative spaces

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