# How to "set a Global Variable" (random) only once and use them for all the keys In the collection

**URL:** <https://community.postman.com/t/how-to-set-a-global-variable-random-only-once-and-use-them-for-all-the-keys-in-the-collection/37800>\
**Category:** Help Hub\
**Tags:** variables, collections, json\
**Created:** [August 2, 2022, 5:05pm UTC](https://community.postman.com/t/how-to-set-a-global-variable-random-only-once-and-use-them-for-all-the-keys-in-the-collection/37800 "2022-08-02T17:05:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnagesh](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@gnagesh](https://community.postman.com/u/gnagesh)\
**Post date:** [August 2, 2022, 5:05pm UTC](https://community.postman.com/t/how-to-set-a-global-variable-random-only-once-and-use-them-for-all-the-keys-in-the-collection/37800/1 "2022-08-02T17:05:15Z")

</div>

I have a request body as follows, Here I need the keys “id”, “sys2Acct” and “shortName” to be set to a same random value.

```auto
{"id":"0372","name":"AN","phone":"","sys2Acct":"7491","shortName":"2592", etc..}

```

NOTE: the request body has huge number of key’s, I somehow want to assign the same number (i.e random) to the mentioned keys in this request and also for the other requests in the collection.

Currently I have written the following script in Collection Pre-Request, But the problem is it is being called every time for the keys, hence resulting in different numbers for the keys

Code in collection pre-request script:

```auto
pm.globals.set("randomNumber", JSON.stringify('{{$randomCreditCardMask}}'));

```

Request scripts:

```auto
"id":{{randomNumber}},"name":"AN","contName":"CN","phone":"","sys2Acct":{{randomNumber}

```

Output: All the keys are getting different numbers.

```auto
{"id":"0372","name":"AN","contName":"CN","phone":"","sys2Acct":"7491","shortName":"2592"}

```

Any help appreciated 🙂

Can you please guide me how to achieve this.
