# Using \_.random. Can I pass a function as a parameter?

**URL:** <https://community.postman.com/t/using-random-can-i-pass-a-function-as-a-parameter/40943>\
**Category:** Help Hub\
**Tags:** scripts\
**Created:** [November 9, 2022, 3:33pm UTC](https://community.postman.com/t/using-random-can-i-pass-a-function-as-a-parameter/40943 "2022-11-09T15:33:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![marina-biro](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@marina-biro](https://community.postman.com/u/marina-biro)\
**Post date:** [November 9, 2022, 3:33pm UTC](https://community.postman.com/t/using-random-can-i-pass-a-function-as-a-parameter/40943/1 "2022-11-09T15:33:10Z")

</div>

I am trying to make a function where I randomly get a date or a string “foo”, but I keep getting zero as a response.

var moment = require(‘moment’);  
var containter = moment().add(\_.random(0, 13),‘months’).format(“MM.YYYY.”);  
var variants = [‘foo’, containter];  
var option = variants[Math.floor(Math.random()\*variants.length)];  
pm.environment.set(“someFoo”, option);

---

<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:** [November 9, 2022, 4:37pm UTC](https://community.postman.com/t/using-random-can-i-pass-a-function-as-a-parameter/40943/2 "2022-11-09T16:37:54Z")

</div>

Seems to work ok for me. This is pretty much the same code in a loop.

```auto
var moment = require("moment");

for (let i = 0; i < 10; i++) {
    var containter = moment().add(_.random(0, 13),"months").format("MM.YYYY.");
    var variants = ["foo", containter];
    var option = variants[Math.floor(Math.random()*variants.length)];
    console.log(option);
}

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/3/633dacde35968377757b0c200ffa58b551fbaf4c.png)

Perhaps check the quotes, as I had to change a few to normal double quotes to get it to work.

---

<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:** [November 9, 2022, 4:43pm UTC](https://community.postman.com/t/using-random-can-i-pass-a-function-as-a-parameter/40943/3 "2022-11-09T16:43:14Z")

</div>

You can also use the lodash sample method instead of the math.random(), array.length and math.floor() method.

The library is built into Postman and you don’t have to declare it.

```auto
var moment = require("moment");

for (let i = 0; i < 10; i++) {
    var containter = moment().add(_.random(0, 13),"months").format("MM.YYYY.");
    var variants = ["foo", containter];
    var option = _.sample(variants);
    console.log(option);
}

```

---

<div class="post-metadata">

**Author:** ![marina-biro](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@marina-biro](https://community.postman.com/u/marina-biro)\
**Post date:** [November 10, 2022, 8:46am UTC](https://community.postman.com/t/using-random-can-i-pass-a-function-as-a-parameter/40943/4 "2022-11-10T08:46:23Z")

</div>

Than you. I see it doesn’t work without it, but why do I need a loop?

I understand it like this

// calculates a random date  
var containter = moment().add(\_.random(0, 13),“months”).format(“MM.YYYY.”);

//defines the variants  
var variants = [“foo”, containter];

//chooses one of the variants  
var option = \_.sample(variants);

//we use the chosen variant  
console.log(option);

---

<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:** [November 10, 2022, 10:23am UTC](https://community.postman.com/t/using-random-can-i-pass-a-function-as-a-parameter/40943/5 "2022-11-10T10:23:08Z")

</div>

You don’t need a loop. I added a loop just to prove that its generating random data.

If you run it manually once or twice, then there is a chance, that it will just show “foo”.

---

<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:** [November 10, 2022, 10:26am UTC](https://community.postman.com/t/using-random-can-i-pass-a-function-as-a-parameter/40943/6 "2022-11-10T10:26:26Z")

</div>

This just repeats the same code four times - no loop.

```auto
    var moment = require("moment");
    
    var containter = moment().add(_.random(0, 13),"months").format("MM.YYYY.");
    var variants = ["foo", containter];
    var option = _.sample(variants);
    console.log(option);

    var containter = moment().add(_.random(0, 13),"months").format("MM.YYYY.");
    var variants = ["foo", containter];
    var option = _.sample(variants);
    console.log(option);

    var containter = moment().add(_.random(0, 13),"months").format("MM.YYYY.");
    var variants = ["foo", containter];
    var option = _.sample(variants);
    console.log(option);

    var containter = moment().add(_.random(0, 13),"months").format("MM.YYYY.");
    var variants = ["foo", containter];
    var option = _.sample(variants);
    console.log(option);

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/5/a5485643a6dcd520fac3d4cc886a760703e2ae42.png)

---

<div class="post-metadata">

**Author:** ![marina-biro](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@marina-biro](https://community.postman.com/u/marina-biro)\
**Post date:** [November 10, 2022, 11:00am UTC](https://community.postman.com/t/using-random-can-i-pass-a-function-as-a-parameter/40943/7 "2022-11-10T11:00:06Z")

</div>

I see. I did get “foo” 4 times, so I thought it doesn’t work. Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [October 12, 2023, 8:49am UTC](https://community.postman.com/t/using-random-can-i-pass-a-function-as-a-parameter/40943/8 "2023-10-12T08:49:49Z")

</div>

Closed on October 12, 2023, 8:49am UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
