Add random value generators
This commit is contained in:
parent
a116462fc5
commit
61cbfd8508
@ -52,6 +52,7 @@ import java.io.InputStreamReader;
|
|||||||
import static java.lang.Math.*;
|
import static java.lang.Math.*;
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.security.SecureRandom;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -83,6 +84,8 @@ public class Functions {
|
|||||||
|
|
||||||
private String lang = "py";
|
private String lang = "py";
|
||||||
|
|
||||||
|
private SecureRandom rng = new SecureRandom();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Useful interactions
|
Useful interactions
|
||||||
*/
|
*/
|
||||||
@ -301,22 +304,52 @@ public class Functions {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all unique permutations of the given array.
|
* Get all unique permutations of the given array.
|
||||||
|
*
|
||||||
* @param objs Array of items.
|
* @param objs Array of items.
|
||||||
* @return Matrix
|
* @return Matrix
|
||||||
*/
|
*/
|
||||||
public Object[] perms(Object... objs) {
|
public Object[] perms(Object... objs) {
|
||||||
Permutations<Object> perm = new Permutations<>(objs);
|
Permutations<Object> perm = new Permutations<>(objs);
|
||||||
|
|
||||||
Set perms = new HashSet<>();
|
Set perms = new HashSet<>();
|
||||||
|
|
||||||
while (perm.hasNext()) {
|
while (perm.hasNext()) {
|
||||||
perms.add(perm.next());
|
perms.add(perm.next());
|
||||||
}
|
}
|
||||||
|
|
||||||
Object[][] a = new Object[perms.size()][objs.length];
|
Object[][] a = new Object[perms.size()][objs.length];
|
||||||
return perms.toArray(a);
|
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.
|
* Multiply two matrices.
|
||||||
*
|
*
|
||||||
|
@ -47,4 +47,10 @@ def times(*a):
|
|||||||
def divide(*a):
|
def divide(*a):
|
||||||
return _.divide(a)
|
return _.divide(a)
|
||||||
def mod(*a):
|
def mod(*a):
|
||||||
return _.mod(a)
|
return _.mod(a)
|
||||||
|
def rand():
|
||||||
|
return _.rand()
|
||||||
|
def rand(min,max):
|
||||||
|
return _.rand(min,max)
|
||||||
|
def randb():
|
||||||
|
return _.randb()
|
||||||
|
@ -16,6 +16,14 @@ plotclr()|Reset the graph.
|
|||||||
drawdot(x, y)|Places a dot at the given coordinates.
|
drawdot(x, y)|Places a dot at the given coordinates.
|
||||||
readfile("")|Read a text file from the given filename.
|
readfile("")|Read a text file from the given filename.
|
||||||
savefile(data,"")|Save the text data to a file.
|
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.
|
abs(0)|Absolute value of number.
|
||||||
asin(0)|
|
asin(0)|
|
||||||
acos(0)|
|
acos(0)|
|
||||||
|
@ -15,6 +15,14 @@ drawdot(x, y)|Places a dot at the given coordinates.
|
|||||||
simplify('')|Simplify the given function.
|
simplify('')|Simplify the given function.
|
||||||
readfile("")|Read a text file from the given filename.
|
readfile("")|Read a text file from the given filename.
|
||||||
savefile(data,"")|Save the text data to a file.
|
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.
|
vpa('')|Computes numerical value or simplifies.
|
||||||
fabs(0)|Absolute value of number.
|
fabs(0)|Absolute value of number.
|
||||||
asin(0)|
|
asin(0)|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user