How to generate random numbers from arrays with pattern?

Viewed 163

I have a groups of numbers which I can select numbers to be included on my array.

Example:

Group 1 [1,2,3,4,5]

Group 2 [11,12,13,14,15]

Group 3 [21,22,23,24,25]

At the moment my code is generating 8 random numbers from all groups.

What I don't know is to generate random numbers with a pattern.

I want to have pattern like this:

2 random numbers from group 1

3 random numbers from group 2

3 random numbers from group 3

This is my javascript at the moment generating 8 random numbers from selected numbers I want in array.

var checkedarray = [];
  var checkboxes = document.querySelectorAll('input[type=checkbox]:checked')

  const d = document.getElementById("vdisplay");

  for (var i = 0; i < checkboxes.length; i++) {
    checkedarray.push(checkboxes[i].value)
}

var chkdarr = [];
  while(chkdarr.length < 7){
  var myrandomnumber2 = checkedarray[Math.floor(Math.random() * checkedarray.length)]
  if(chkdarr.indexOf(myrandomnumber2) === -1) chkdarr.push(myrandomnumber2);
 };

// To generate another 8 set of numbers but along with the existing set of numbers.
d.innerHTML = d.innerHTML + (d.innerHTML === "" ? "" : "<br>") + chkdarr.join(", ");
3 Answers

Works perfectly ;)

let group1 = [1,2,3,4,5];

let group2 = [11,12,13,14,15];

let group3 =[21,22,23,24,25];

//Place all arrays with their order 1st 2nd etc.. in a matrix like so

let allArrsInOne = [];

allArrsInOne.push(group1);
allArrsInOne.push(group2);
allArrsInOne.push(group3);

//See your All arrays

//console.log(allArrsInOne);

//Create an array of pattern

let pattern = [2,3,3];

let result = [];

//add also checked array not to add the same number twice
let checked = [];


for(let i = 0;i<pattern.length;i++){
  for(let j = 0;j<pattern[i];j++){
    let randomFromI = Math.floor(Math.random()*allArrsInOne.length);
    //This while loop creates a new random number untill it will find one that wasn't used
    while(checked.includes(allArrsInOne[i][randomFromI])){
      randomFromI = Math.floor(Math.random()*allArrsInOne.length);
    }
    //Then we add that never used random number's index to our result 
    result.push(allArrsInOne[i][randomFromI]);
    //add to checked not to add already added number
    checked.push(allArrsInOne[i][randomFromI]);
  }
}
console.log(result);

Should be something like this:

let groupOne = [1,2,3,4,5]
let groupTwo = [11,12,13,14,15]
let groupThree = [21,22,23,24,25]

let randomNum, result = []

for(i=1;i<=3;i++){
  if(i<3){
     randomNum = groupOne[Math.floor(Math.random() * groupOne.length)]
     if( ! result.includes(randomNum) ){
      result.push(randomNum)
     }
  }
  
  randomNum = groupTwo[Math.floor(Math.random() * groupTwo.length)]
  if( ! result.includes(randomNum) ){
      result.push(randomNum)
    }
    
    
   randomNum = groupThree[Math.floor(Math.random() * groupThree.length)]
  if( ! result.includes(randomNum) ){
      result.push(randomNum)
    }
   
}

console.log(result)

here is a very simple function where you put the group and how many random numbers you want and it generates an array of unique random numbers

Group1 = [1,2,3,4,5] 
Group2 = [11,12,13,14,15]
Group3 = [21,22,23,24,25]

function generaterand(group,reac){
            rand=[]
           for(let i=0;i<reac;i++){
             randnumb=group[Math.floor(Math.random()*group.length)]
             !rand.includes(randnumb)?rand.push(randnumb):generaterand(group,reac)
             }
           return rand
}
console.log(generaterand(Group1,2))
Related