diff --git a/src/net/apocalypselabs/symat/Functions.java b/src/net/apocalypselabs/symat/Functions.java index 4ec00aa..c0506ba 100644 --- a/src/net/apocalypselabs/symat/Functions.java +++ b/src/net/apocalypselabs/symat/Functions.java @@ -52,6 +52,7 @@ import java.io.InputStreamReader; import static java.lang.Math.*; import java.lang.reflect.Array; import java.net.URL; +import java.security.SecureRandom; import java.util.Arrays; import java.util.Calendar; import java.util.Date; @@ -83,6 +84,8 @@ public class Functions { private String lang = "py"; + private SecureRandom rng = new SecureRandom(); + /* Useful interactions */ @@ -301,22 +304,52 @@ public class Functions { /** * Get all unique permutations of the given array. + * * @param objs Array of items. * @return Matrix */ public Object[] perms(Object... objs) { Permutations perm = new Permutations<>(objs); - + Set perms = new HashSet<>(); - + while (perm.hasNext()) { perms.add(perm.next()); } - + Object[][] a = new Object[perms.size()][objs.length]; return perms.toArray(a); } + /** + * Get a uniform random fraction between 0.0 (inclusive) and 1.0 + * (exclusive). + * + * @return random fraction + */ + public double rand() { + return rng.nextDouble(); + } + + /** + * Get a random boolean value. + * @return true or false + */ + public boolean randb() { + return rng.nextBoolean(); + } + + /** + * Get a uniform random integer. + * + * @param min Minimum value, inclusive + * @param max Maximum value, inclusive + * @return random integer + */ + public int rand(int min, int max) { + return rng.nextInt((max - min) + 1) + min; + } + /** * Multiply two matrices. * diff --git a/src/net/apocalypselabs/symat/functions.py b/src/net/apocalypselabs/symat/functions.py index a4917be..858520d 100644 --- a/src/net/apocalypselabs/symat/functions.py +++ b/src/net/apocalypselabs/symat/functions.py @@ -47,4 +47,10 @@ def times(*a): def divide(*a): return _.divide(a) def mod(*a): - return _.mod(a) \ No newline at end of file + return _.mod(a) +def rand(): + return _.rand() +def rand(min,max): + return _.rand(min,max) +def randb(): + return _.randb() diff --git a/src/net/apocalypselabs/symat/resources/jsfunctions.txt b/src/net/apocalypselabs/symat/resources/jsfunctions.txt index 25234c1..eb691d4 100644 --- a/src/net/apocalypselabs/symat/resources/jsfunctions.txt +++ b/src/net/apocalypselabs/symat/resources/jsfunctions.txt @@ -16,6 +16,14 @@ plotclr()|Reset the graph. drawdot(x, y)|Places a dot at the given coordinates. readfile("")|Read a text file from the given filename. savefile(data,"")|Save the text data to a file. +rand()|Random fraction from 0 (inclusive) to 1 (exclusive) +rand(min,max)|Random number between min and max, inclusive +randb()|Random boolean (true or false) +add(n1,n2,...)|Add the given numbers. +subtract(n1,n2,...)|Subtract the given numbers. +times(n1,n2,...)|Multiply the given numbers. +divide(n1,n2,...)|Divide the given numbers. +mod(n1,n2,...)|Divide the numbers and return the remainder. abs(0)|Absolute value of number. asin(0)| acos(0)| diff --git a/src/net/apocalypselabs/symat/resources/pyfunctions.txt b/src/net/apocalypselabs/symat/resources/pyfunctions.txt index feea00f..e7784f1 100644 --- a/src/net/apocalypselabs/symat/resources/pyfunctions.txt +++ b/src/net/apocalypselabs/symat/resources/pyfunctions.txt @@ -15,6 +15,14 @@ drawdot(x, y)|Places a dot at the given coordinates. simplify('')|Simplify the given function. readfile("")|Read a text file from the given filename. savefile(data,"")|Save the text data to a file. +rand()|Random fraction from 0 (inclusive) to 1 (exclusive) +rand(min,max)|Random number between min and max, inclusive +randb()|Random boolean (true or false) +add(n1,n2,...)|Add the given numbers. +subtract(n1,n2,...)|Subtract the given numbers. +times(n1,n2,...)|Multiply the given numbers. +divide(n1,n2,...)|Divide the given numbers. +mod(n1,n2,...)|Divide the numbers and return the remainder. vpa('')|Computes numerical value or simplifies. fabs(0)|Absolute value of number. asin(0)|