Write a function that returns an array of integers with 1000 elements containing the values 1 to 1000 in random order. No number can be repeated or omitted.Your answer should show good programming style, technique and attention to accuracy and performance.
Hint: use Random rnd=new Random() to create a new instance of the random number generator and rnd.Next(1,1000) to get a random integer value between 1 and 1000. No other .Net framework classes should be needed outside of the intrinsic data types (i.e. DO NOT use collections).
For that I have developed the following code:
import java.util.Random;
public class Domain {
public int[] randomIntArray(int size) {
Random random = new Random();
int intArrayHolder[] = new int[size];
for (int i = 0; i < intArrayHolder.length; i++) {
boolean isDuplicate = false;
/*
* because random.nextInt(int var) return between 0 and given-no and
* we do not want 0. So size +1 and later in program, we do not
* assign 0 to the intArrayHolder.
*/
int randomNextInt = random.nextInt(size + 1);
for (int j = 0; j < i; j++) {
if (intArrayHolder[j] == randomNextInt) {
isDuplicate = true;
break;
}
}
if ((!isDuplicate) && (randomNextInt != 0)) {
intArrayHolder[i] = randomNextInt;
} else {
i--;
}
}
return intArrayHolder;
}
}
JUnit test class:
import java.util.HashSet;
import java.util.Set;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class DomainJunitTest {
Domain domain = null;
int arraySize = 0;
@Before
public void initialize() throws Exception {
domain = new Domain();
arraySize = 1000;
}
@Test
public void randomIntArray_size_check() {
int intArrayHolder[] = domain.randomIntArray(arraySize);
Assert.assertEquals(arraySize, intArrayHolder.length);
}
@Test
public void randomIntArray_duplicate_value_check() throws Exception{
int intArrayHolder[] = domain.randomIntArray(arraySize);
Set<Integer> intSet = new HashSet<>();
boolean isDublicate = false;
for (int i = 0; i < intArrayHolder.length; i++) {
if (intSet.contains(intArrayHolder[i])) {
isDublicate=true;
}else{
intSet.add(intArrayHolder[i]);
}
}
Assert.assertEquals(false, isDublicate);
}
@Test
public void randomIntArray_size_check_after_removing_dublicate() {
int intArrayHolder[] = domain.randomIntArray(arraySize);
Set<Integer> intSet = new HashSet<>();
for (int i = 0; i < intArrayHolder.length; i++) {
intSet.add(intArrayHolder[i]);
}
Assert.assertEquals(arraySize, intSet.size());
}
}
And I am being advised it is not good enough. Can someone point out where I went wrong?
java.util
classes? You could simply create a list/set of 1000 integers in ascending order and callCollections.shuffle()
to randomize the list/set. It's no more than 5 lines of code. – GiantTree 21 hours agoCollections.shuffle()
would need to have ObjectIntegers
, not primitiveint
values - though some 'simple' manipulations would get you there. – rolfl 21 hours agoArrays
does not have 'shuffle' methods just so that teachers have something they can add to their courses ;-) – rolfl 20 hours ago