Created "natural" method in "MathArrays" from code previously in

class "RandomDataGenerator" (private method).


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1520619 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2013-09-06 16:05:51 +00:00
parent 1f72a13a37
commit db77aa6030
2 changed files with 32 additions and 0 deletions

View File

@ -1533,4 +1533,19 @@ public class MathArrays {
public static void shuffle(int[] list) {
shuffle(list, new Well19937c());
}
/**
* Returns an array representing the natural number {@code n}.
*
* @param n Natural number.
* @return an array whose entries are the numbers 0, 1, ..., {@code n}-1.
* If {@code n == 0}, the returned array is empty.
*/
public static int[] natural(int n) {
final int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = i;
}
return a;
}
}

View File

@ -979,4 +979,21 @@ public class MathArraysTest {
}
Assert.assertTrue(ok);
}
@Test
public void testNatural() {
final int n = 4;
final int[] expected = {0, 1, 2, 3};
final int[] natural = MathArrays.natural(n);
for (int i = 0; i < n; i++) {
Assert.assertEquals(expected[i], natural[i]);
}
}
@Test
public void testNaturalZero() {
final int[] natural = MathArrays.natural(0);
Assert.assertEquals(natural.length, 0);
}
}