Ben Chuanlong Du's Blog

It is never too late to learn.

Hands on the RandomDataGenerator class in Apache Commons Math

In [2]:
%classpath add mvn org.apache.commons commons-math3 3.6.1
In [3]:
import org.apache.commons.math3.random.RandomDataGenerator
import collection.JavaConverters._
val rng = new RandomDataGenerator()
Out[3]:
scala.collection.JavaConverters$@783d0556

Notice that the both of the endpoints are included, which is different from typcial Java conventions!!!

In [4]:
rng.nextInt(0, 2)
Out[4]:
2
In [5]:
rng.nextInt(0, 1)
Out[5]:
1
In [7]:
rng.nextInt(0, 1)
Out[7]:
0
In [3]:
rng.nextPermutation(5, 3)
Out[3]:
[4, 3, 0]
In [4]:
rng.nextPermutation(5, 3)
Out[4]:
[2, 3, 0]
In [14]:
val vec = Vector(10, 20, 30, 40, 50)
Out[14]:
[[10, 20, 30, 40, 50]]
In [19]:
rng.nextSample(vec.asJava, 3).mkString(" ")
Out[19]:
50 20 10
In [20]:
rng.nextSample(vec.asJava, 3).mkString(" ")
Out[20]:
50 30 40

Comments