I need to generate a different array each time in JS , where it's length should be exact 16 elements , each element should be between 0 to 7 , and each element should occur exactly twice. for eg. [0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7] or [0,1,2,3,4,5,6,7,7,6,5,4,3,2,1,0]
I need a function that will generate different array each time.
let sequence=[0,1,2,3,4,5,6,7]
let seq1=[0,1,2,3,4,5,6,7]
let seq2=[0,1,2,3,4,5,6,7]
let arr1=[]
let arr2=[]
let arr3
function generateArray(arr,seq){
for(let i=0;i<8;i++){
let numb=Math.trunc(Math.random()*(seq.length))
arr.push(seq[numb])
seq.splice(numb,1)
}
}
generateArray(arr1,seq1)
generateArray(arr2,seq2)
arr3=[...arr1,...arr2]
console.log(arr3)
I've used the above function , but the drawback is that it firsts generates randomly all the numbers from 0 to 7 , and then does the same again.