Suppose I have 16 numbers, range 0 to 15. They sit in an array. What I want to achieve is the following arrays:
- Array with 3 random numbers.
- Array with 2 random numbers.
- Array with 3 random numbers.
- Array with 2 random numbers.
- Array with 2 random numbers.
- Array with 3 random numbers.
The numbers from the, what I will call, masterarray, can only be used once. So array x holds 1, 5 and 7, array y holds 2, 9 (these should be totally random).
I understand that I can get and remove items from an array using indexOf
and splice
. But how would I go about getting the random numbers from the array while also removing them from this list?
A foreach wouldn't go as the array gets altered in this process. Same goes for a for-loop, to my understanding.
How can I achieve this?