V. Vais
V. Vais

Reputation: 86

Preventing of adding same random values in array

I am making a feature where random indexes are added to an array with for-loop, values must be always different but somehow there are sometimes (approximately every 10. call of function) same values even I am trying to filter same values from the answerId-array and put rest of values unique-array and check are filtered unique-array and original answerId-array having same length, if unique-arrays length is lower than unique-arrays length, answerIds values will be changed. But these arrays have always same length even if there are same values in answerId-array and running cannot pass to rest of for-loops (those for-loops are quite some hack code). What I am doing wrong? Sorry if my english skills are not good.

getJSONData(){

  var answerId = [null, null, null, null];
  var length = Object.keys(Company.person).length;

  for(var i = 0; i <= answerId.length - 1; i++){
    answerId[i] = [Math.floor(Math.random() * length)]

  }

  let unique = Array.from(new Set(answerId))
  console.log(unique.length)

  if (unique.length < answerId.length){
    for(var i = 0; i <= answerId.length - 1; i++){
      answerId[i] = [Math.floor(Math.random() * length)]
    }
    console.log("new values 1")
    unique = Array.from(new Set(answerId))

    if (unique.length < answerId.length){
      for(var i = 0; i <= answerId.length - 1; i++){
        answerId[i] = [Math.floor(Math.random() * length)]
      }
      console.log("new values 2")
      unique = Array.from(new Set(answerId))

      if (unique.length < answerId.length){
        for(var i = 0; i <= answerId.length - 1; i++){
          answerId[i] = [Math.floor(Math.random() * length)]
        }
        console.log("new values 3")
        unique = Array.from(new Set(answerId))

        if (unique.length < answerId.length){
          for(var i = 0; i <= answerId.length - 1; i++){
            answerId[i] = [Math.floor(Math.random() * length)]
          }
          console.log("new values 4")
        }
      }
    }
  }

  var personArray = [Company.person[answerId[0]].firstName + ' ' + Company.person[answerId[0]].lastName, Company.person[answerId[1]].firstName + ' ' + Company.person[answerId[1]].lastName, Company.person[answerId[2]].firstName + ' ' + Company.person[answerId[2]].lastName, Company.person[answerId[3]].firstName + ' ' + Company.person[answerId[3]].lastName];
  return personArray;
}

Upvotes: 2

Views: 59

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386560

You could use directly Set with size property for checking the wanted size of the result set.

var array = [0, 1, 42, 17, 22, 3, 7, 9, 15, 35, 20],
    unique = new Set;

while (unique.size < 4) {
    unique.add(Math.floor(Math.random() * array.length));
}

console.log([...unique]); // indices

Otherwise, you could use a hash table.

var array = [0, 1, 42, 17, 22, 3, 7, 9, 15, 35, 20],
    unique = {},
    id = [],
    i;

while (id.length < 4) {
    i = Math.floor(Math.random() * array.length);
    if (!unique[i]) {
        id.push(i);
        unique[i] = true;
    }
}

console.log(id);

Upvotes: 1

br3t
br3t

Reputation: 1658

As I understood your problem, you want to get N random values from array, values should be unique. You've just shuffle you array and get first N values from it.

Upvotes: 1

Related Questions