Built-in Random Variables

FOR COMMERCIAL USE, PLEASE ENSURE YOU HAVE A COMMERCIAL LICENSE.
ANOTHER WAY TO SUPPORT MY WORK IS BY PURCHASING MY BOOK, CUSTOMER FIRST, VALUE NEXT.
THANK YOU FOR SUPPORTING MY WORK!

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 integer

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("[Int]");

mXparser.consolePrintln("Res. 1: " + e.getExpressionString() + " = " + (int)e.calculate());
mXparser.consolePrintln("Res. 2: " + e.getExpressionString() + " = " + (int)e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("[Int]");

mXparser::consolePrintln("Res. 1: " + e->getExpressionString() + " = " + (int)e->calculate());
mXparser::consolePrintln("Res. 2: " + e->getExpressionString() + " = " + (int)e->calculate());
Code result
[mXparser-v.5.2.1] Res. 1: [Int] = -421216764
[mXparser-v.5.2.1] Res. 2: [Int] = 859703054

Case 2: Random integer N: -10^k <= N <= 10^k for k = 1, 2, …,9

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...

/* -100 <= N <= 100 */
Expression e2 = new Expression("[Int2]");

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

/* -100000 <= N <= 100000 */
Expression e5 = new Expression("[Int5]");

mXparser.consolePrintln("Res. 3: " + e5.getExpressionString() + " = " + e5.calculate());
mXparser.consolePrintln("Res. 4: " + e5.getExpressionString() + " = " + e5.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
/* -100 <= N <= 100 */
ExpressionPtr e2 = new_Expression("[Int2]");

mXparser::consolePrintln("Res. 1: " + e2->getExpressionString() + " = " + e2->calculate());
mXparser::consolePrintln("Res. 2: " + e2->getExpressionString() + " = " + e2->calculate());

/* -100000 <= N <= 100000 */
ExpressionPtr e5 = new_Expression("[Int5]");

mXparser::consolePrintln("Res. 3: " + e5->getExpressionString() + " = " + e5->calculate());
mXparser::consolePrintln("Res. 4: " + e5->getExpressionString() + " = " + e5->calculate());
Code result
[mXparser-v.5.2.1] Res. 1: [Int2] = 92.0
[mXparser-v.5.2.1] Res. 2: [Int2] = 94.0
[mXparser-v.5.2.1] Res. 3: [Int5] = 50442.0
[mXparser-v.5.2.1] Res. 4: [Int5] = -76163.0

Case 3: Random natural number

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...

/* Including 0 */
Expression e1 = new Expression("[nat]");

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

/* Excluding 0 */
Expression e2 = new Expression("[Nat]");

mXparser.consolePrintln("Res. 3: " + e2.getExpressionString() + " = " + (int)e2.calculate());
mXparser.consolePrintln("Res. 4: " + e2.getExpressionString() + " = " + (int)e2.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...

/* Including 0 */
ExpressionPtr e1 = new_Expression("[nat]");

mXparser::consolePrintln("Res. 1: " + e1->getExpressionString() + " = " + (int)e1->calculate());
mXparser::consolePrintln("Res. 2: " + e1->getExpressionString() + " = " + (int)e1->calculate());

/* Excluding 0 */
ExpressionPtr e2 = new_Expression("[Nat]");

mXparser::consolePrintln("Res. 3: " + e2->getExpressionString() + " = " + (int)e2->calculate());
mXparser::consolePrintln("Res. 4: " + e2->getExpressionString() + " = " + (int)e2->calculate());
Code result
[mXparser-v.5.2.1] Res. 1: [nat] = 1659809708
[mXparser-v.5.2.1] Res. 2: [nat] = 1259835535
[mXparser-v.5.2.1] Res. 3: [Nat] = 248106924
[mXparser-v.5.2.1] Res. 4: [Nat] = 1953526897

Case 4: Random natural number N <= 10^k for k = 1, 2, …,9

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...

/* Including 0: 0 <= N < 10 */
Expression e1 = new Expression("[nat1]");

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

/* Excluding 0: 0 < N < 1000 */
Expression e2 = new Expression("[Nat3]");

mXparser.consolePrintln("Res. 3: " + e2.getExpressionString() + " = " + (int)e2.calculate());
mXparser.consolePrintln("Res. 4: " + e2.getExpressionString() + " = " + (int)e2.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...

/* Including 0: 0 <= N < 10 */
ExpressionPtr e1 = new_Expression("[nat1]");

mXparser::consolePrintln("Res. 1: " + e1->getExpressionString() + " = " + (int)e1->calculate());
mXparser::consolePrintln("Res. 2: " + e1->getExpressionString() + " = " + (int)e1->calculate());

/* Excluding 0: 0 < N < 1000 */
ExpressionPtr e2 = new_Expression("[Nat3]");

mXparser::consolePrintln("Res. 3: " + e2->getExpressionString() + " = " + (int)e2->calculate());
mXparser::consolePrintln("Res. 4: " + e2->getExpressionString() + " = " + (int)e2->calculate());
Code result
[mXparser-v.5.2.1] Res. 1: [nat1] = 10
[mXparser-v.5.2.1] Res. 2: [nat1] = 0
[mXparser-v.5.2.1] Res. 3: [Nat3] = 90
[mXparser-v.5.2.1] Res. 4: [Nat3] = 51

Case 5: Uniform continuous distribution U(0,1)

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("[Uni]");

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("[Uni]");

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: [Uni] = 0.9203947484394417
[mXparser-v.5.2.1] Res. 2: [Uni] = 0.27088514104678163

Case 6: Normal distribution N(0,1)

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("[Nor]");

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("[Nor]");

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: [Nor] = 0.33914374337258346
[mXparser-v.5.2.1] Res. 2: [Nor] = 0.2480653909819784
Nuget – Package Manager (C#, F#, Visual Basic, …)

Install-Package MathParser.org-mXparser-Version 6.1.1

Nuget – .NET CLI

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

Nuget – Package Reference

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

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

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

Maven – Gradle

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

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.1
SOURCE_SUBDIR CURRENT/cpp/lib
)
FetchContent_MakeAvailable(
MathParserOrgMxParser)
mxparser_link(YourExecutable)

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