Set variable a random integer but exclude some values from an earlier response

Hi all, I am new to Postman and still trying to get my head around it :slight_smile:

My case:
From request 1 I get in json response some values e.g.
{“number”: 70},
{“number”: 2},
{“number”: -1},

In request 2 I am setting a random number in pre-request script like this:
var randomFloorNo = pm.variables.set(“randomFloorNo”, _.random (-10,99));
and call the variable in request body like this:
{
“floorNumber”: {{randomFloorNo}},
}

My question:
I need to set the randomFloorNo in request 2 but excluding the values that I receive in request one.
e.g. set randomFloorNo any value between -10 and 99 but exclude the values 70, 2, -1

Any ideas how I can accomplish this?

Many thanks in advance.

Taken from here.

JavaScript generate random number except some values - Stack Overflow

function generateRandom(min, max, exclude) {
  let random;
  while (!random) {
    const x = Math.floor(Math.random() * (max - min + 1)) + min;
    if (exclude.indexOf(x) === -1) random = x;
  }
  return random;
}

for (let i = 0; i < 25; i++) {
    console.log(generateRandom(-10, 99, [2, -1]));
}

image

@michaelderekjones thanks for the quick reply!
I found a solution in the meantime:

var existingFloorNo = [
{“floorNumber”: 22},
{“floorNumber”: 5},
{“floorNumber”: -2}
]

const randomFloorNo = (existingFloorNo) => {
const random = Math.floor(Math.random() * 109) + -10;
if(!existingFloorNo.includes(random) ){
return random;
}
return randomFloorNo(existingFloorNo);
}

console.log(randomFloorNo(existingFloorNo));