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

**URL:** <https://community.postman.com/t/set-variable-a-random-integer-but-exclude-some-values-from-an-earlier-response/44658>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [March 2, 2023, 10:59am UTC](https://community.postman.com/t/set-variable-a-random-integer-but-exclude-some-values-from-an-earlier-response/44658 "2023-03-02T10:59:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Silvix](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@Silvix](https://community.postman.com/u/Silvix)\
**Post date:** [March 2, 2023, 10:59am UTC](https://community.postman.com/t/set-variable-a-random-integer-but-exclude-some-values-from-an-earlier-response/44658/1 "2023-03-02T10:59:24Z")

</div>

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

**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.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [March 2, 2023, 12:52pm UTC](https://community.postman.com/t/set-variable-a-random-integer-but-exclude-some-values-from-an-earlier-response/44658/2 "2023-03-02T12:52:50Z")

</div>

Taken from here.

[JavaScript generate random number except some values - Stack Overflow](https://stackoverflow.com/questions/27406377/javascript-generate-random-number-except-some-values)

```auto
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](https://us1.discourse-cdn.com/getpostman/original/3X/4/3/43128e4d7274a4e555e608b324c2387852f9a014.png)

---

<div class="post-metadata">

**Author:** ![Silvix](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@Silvix](https://community.postman.com/u/Silvix)\
**Post date:** [March 2, 2023, 1:19pm UTC](https://community.postman.com/t/set-variable-a-random-integer-but-exclude-some-values-from-an-earlier-response/44658/3 "2023-03-02T13:19:46Z")

</div>

@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));
