user3279174 - 2 months ago 10

Java Question

I need to generate random numbers starting from 1 to 100 with a repetition factor of 7 in java.

i.e., each number should repeat exactly for 7 times. So, i need to generate 700 numbers in total.

Could anyone please help me how to proceed?

Thanks in advance...

Answer

Check this

```
ArrayList list1 = new ArrayList();
Random rnd = new Random();
for (int i = 0; i < 7; i++) {
ArrayList list2 = new ArrayList();
while(list2.size() != 100) {
int num = rnd.nextInt(101);
if( num == 0 || list2.contains(num)) {
continue;
}
list2.add(num);
}
list1.addAll(list2);
}
System.out.println(list1.size());
```

list2 contains subeset of random numbers between 1 to 100 and list1 contains 7 times list2.

Source (Stackoverflow)

Comments