Built-in Random Variables

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 integer

import 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());
[mXparser-v.3.0.0] Res. 1: [Int] = -703919150
[mXparser-v.3.0.0] Res. 2: [Int] = 695452416

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

import 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());
[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 natural number

import 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());
[mXparser-v.3.0.0] Res. 1: [nat] = 937072002
[mXparser-v.3.0.0] Res. 2: [nat] = 486839726
[mXparser-v.3.0.0] Res. 3: [Nat] = 1087355458
[mXparser-v.3.0.0] Res. 4: [Nat] = 1858414746

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

import 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());
[mXparser-v.3.0.0] Res. 1: [nat1] = 9
[mXparser-v.3.0.0] Res. 2: [nat1] = 4
[mXparser-v.3.0.0] Res. 3: [Nat3] = 909
[mXparser-v.3.0.0] Res. 4: [Nat3] = 81

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

import 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());
[mXparser-v.3.0.0] Res. 1: [Uni] = 0.013090988382426616
[mXparser-v.3.0.0] Res. 2: [Uni] = 0.23135167952988556

Case 6: Normal distribution N(0,1)

import 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());
[mXparser-v.3.0.0] Res. 1: [Nor] = 0.12696847671064085
[mXparser-v.3.0.0] Res. 2: [Nor] = 0.5540874141380246

*** 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