<div dir="ltr">Hey Ben, <div><br></div><div>Something like that is exactly what I was looking for, and you just gave me the headstart I needed! Appreciate the snippet!</div><div><br></div><div>Best, </div><div>Joel.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, May 4, 2022 at 4:06 PM Ben Kaufman <<a href="mailto:bkaufman@bcmone.com">bkaufman@bcmone.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Without any external dependencies.  Not sure how you're populating your pool of numbers for the caller id, but this generally works:<br>
<br>
```<br>
    ## Populate your list however you want:<br>
    $(avp(cid)) = "15555551000";<br>
    $(avp(cid)) = "15555551001";<br>
    $(avp(cid)) = "15555551002";<br>
    $(avp(cid)) = "15555551003";<br>
    $(avp(cid)) = "15555551004";<br>
    $(avp(cid)) = "15555551005";<br>
    $(avp(cid)) = "15555551006";<br>
    ## $avp(cid) now holds 7 values.<br>
<br>
    ## A random integer using the remainder $RANDOM divided by the number of<br>
    ## elements:<br>
    $var(rand)     = $RANDOM mod $cnt($avp(cid));<br>
<br>
    ## Use the random integer to get the CID out of the list<br>
    $var(rand_cid) = $(avp(cid)[$var(rand)]);<br>
```<br>
<br>
<br>
Ben Kaufman<br>
Sr. VoIP Engineer - BCM One<br>
<br>
+1.612.735.9309<br>
<a href="mailto:bkaufman@bcmone.com" target="_blank">bkaufman@bcmone.com</a><br>
24 hour client support: +1.855.639.6300<br>
<br>
-----Original Message-----<br>
From: sr-users <<a href="mailto:sr-users-bounces@lists.kamailio.org" target="_blank">sr-users-bounces@lists.kamailio.org</a>> On Behalf Of Fred Posner<br>
Sent: Wednesday, May 4, 2022 3:02 PM<br>
To: <a href="mailto:sr-users@lists.kamailio.org" target="_blank">sr-users@lists.kamailio.org</a><br>
Subject: Re: [SR-Users] Tips on picking random element from a list<br>
<br>
If you were choosing random numbers from a pool of numbers, you could always lets say store the caller-id's in redis, connect kamailio to redis, and just use the RANDOMKEY function of redis to return a caller id.<br>
<br>
Fred Posner | <a href="http://palner.com" rel="noreferrer" target="_blank">palner.com</a><br>
o: +1 (212) 937-7844<br>
<br>
On 5/4/22 15:43, Ben Kaufman wrote:<br>
> How "random" do you need to be here? Given your example, a quick <br>
> thought would be to just iterate over a list, using a counter and <br>
> calculating the modulo (remainder) when dividing it by the number of elements you have.<br>
> <br>
> Another thought would be to use the core_hash() function over the <br>
> call-id.<br>
> <a href="https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.k" rel="noreferrer" target="_blank">https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.k</a><br>
> <a href="http://amailio.org" rel="noreferrer" target="_blank">amailio.org</a>%2Fdocs%2Fmodules%2F5.6.x%2Fmodules%2Fcfgutils.html%23cfgut<br>
> ils.f.core_hash&amp;data=05%7C01%7Cbkaufman%<a href="http://40bcmone.com" rel="noreferrer" target="_blank">40bcmone.com</a>%7C233860f6470<br>
> 7428040a008da2e090ef7%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C637<br>
> 872913679726971%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2<br>
> luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=zdoWq%2<br>
> FANIgusBipnbAD4%2BJpDzMc6fNrilEawt5rV2f8%3D&amp;reserved=0<br>
> <<a href="https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww" rel="noreferrer" target="_blank">https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww</a>.<br>
> <a href="http://kamailio.org" rel="noreferrer" target="_blank">kamailio.org</a>%2Fdocs%2Fmodules%2F5.6.x%2Fmodules%2Fcfgutils.html%23cfgu<br>
> tils.f.core_hash&amp;data=05%7C01%7Cbkaufman%<a href="http://40bcmone.com" rel="noreferrer" target="_blank">40bcmone.com</a>%7C233860f647<br>
> 07428040a008da2e090ef7%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C63<br>
> 7872913679726971%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV<br>
> 2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=zdoWq%<br>
> 2FANIgusBipnbAD4%2BJpDzMc6fNrilEawt5rV2f8%3D&amp;reserved=0><br>
> <br>
> Of course, those aren't really "random" in the mathematical sense, but <br>
> if you're goal is "you have a pool of 10 numbers and want them to be <br>
> assigned to the p-asserted-identity URI user each one-tenth of the <br>
> time", either of the above methods should work.  Hashing the call-id <br>
> would even ensure that the same call will get the same value if you <br>
> have to recalculate it.<br>
> <br>
> *Ben Kaufman*<br>
> <br>
> /Sr. VoIP Engineer - BCM One/<br>
> <br>
> +1.612.735.9309<br>
> <br>
> <a href="mailto:bkaufman@bcmone.com" target="_blank">bkaufman@bcmone.com</a> <mailto:<a href="mailto:bkaufman@bcmone.com" target="_blank">bkaufman@bcmone.com</a>><br>
> <br>
> 24 hour client support: +1.855.639.6300<br>
> <br>
> *From:* sr-users <<a href="mailto:sr-users-bounces@lists.kamailio.org" target="_blank">sr-users-bounces@lists.kamailio.org</a>> *On Behalf Of <br>
> *Joel Serrano<br>
> *Sent:* Tuesday, May 3, 2022 8:40 PM<br>
> *To:* Henning Westerholt <<a href="mailto:hw@gilawa.com" target="_blank">hw@gilawa.com</a>><br>
> *Cc:* Kamailio (SER) - Users Mailing List <br>
> <<a href="mailto:sr-users@lists.kamailio.org" target="_blank">sr-users@lists.kamailio.org</a>><br>
> *Subject:* Re: [SR-Users] Tips on picking random element from a list<br>
> <br>
> I did, but I don't see how to limit the range:<br>
> <br>
>  From the docs: Returns a random value from the [0 - 2^31) range.<br>
> <br>
> Is there a way to limit the range from 1-100 for example? Or maybe the <br>
> way is to use $RANDOM and just rely on the first/last 2 digits <br>
> (essentially a 00-99 range)?<br>
> <br>
> On Tue, May 3, 2022 at 10:40 AM Henning Westerholt <<a href="mailto:hw@gilawa.com" target="_blank">hw@gilawa.com</a> <br>
> <mailto:<a href="mailto:hw@gilawa.com" target="_blank">hw@gilawa.com</a>>> wrote:<br>
> <br>
>     Hello,<br>
> <br>
>     what about just using this PV:<br>
>     <a href="https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fkamailio.org%2Fdocs%2Fmodules%2F5.5.x%2Fmodules%2Fcfgutils.html%23idm438&amp;data=05%7C01%7Cbkaufman%40bcmone.com%7C233860f64707428040a008da2e090ef7%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C637872913679726971%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=qW9OEMTf1MeQl%2FRKqRLah4WLQAogKViHQeS%2FRZTqgN0%3D&amp;reserved=0" rel="noreferrer" target="_blank">https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fkamailio.org%2Fdocs%2Fmodules%2F5.5.x%2Fmodules%2Fcfgutils.html%23idm438&amp;data=05%7C01%7Cbkaufman%40bcmone.com%7C233860f64707428040a008da2e090ef7%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C637872913679726971%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=qW9OEMTf1MeQl%2FRKqRLah4WLQAogKViHQeS%2FRZTqgN0%3D&amp;reserved=0</a><br>
>     <br>
> <<a href="https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fkam" rel="noreferrer" target="_blank">https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fkam</a><br>
> <a href="http://ailio.org" rel="noreferrer" target="_blank">ailio.org</a>%2Fdocs%2Fmodules%2F5.5.x%2Fmodules%2Fcfgutils.html%23idm438&<br>
> amp;data=05%7C01%7Cbkaufman%<a href="http://40bcmone.com" rel="noreferrer" target="_blank">40bcmone.com</a>%7C233860f64707428040a008da2e0<br>
> 90ef7%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C637872913679726971%<br>
> 7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik<br>
> 1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=qW9OEMTf1MeQl%2FRKqRLah<br>
> 4WLQAogKViHQeS%2FRZTqgN0%3D&amp;reserved=0><br>
> <br>
>     Cheers,<br>
> <br>
>     Henning<br>
> <br>
>     --<br>
> <br>
>     Henning Westerholt - <a href="https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fskalatan.de%2Fblog%2F&amp;data=05%7C01%7Cbkaufman%40bcmone.com%7C233860f64707428040a008da2e090ef7%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C637872913679726971%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=xLGoPpD2479CdKjMhiWY67lLq4c8TqmyurLTepGqQTo%3D&amp;reserved=0" rel="noreferrer" target="_blank">https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fskalatan.de%2Fblog%2F&amp;data=05%7C01%7Cbkaufman%40bcmone.com%7C233860f64707428040a008da2e090ef7%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C637872913679726971%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=xLGoPpD2479CdKjMhiWY67lLq4c8TqmyurLTepGqQTo%3D&amp;reserved=0</a><br>
>     <br>
> <<a href="https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fska" rel="noreferrer" target="_blank">https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fska</a><br>
> <a href="http://latan.de" rel="noreferrer" target="_blank">latan.de</a>%2Fblog%2F&amp;data=05%7C01%7Cbkaufman%<a href="http://40bcmone.com" rel="noreferrer" target="_blank">40bcmone.com</a>%7C233860f6<br>
> 4707428040a008da2e090ef7%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C<br>
> 637872913679726971%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjo<br>
> iV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=xLGo<br>
> PpD2479CdKjMhiWY67lLq4c8TqmyurLTepGqQTo%3D&amp;reserved=0><br>
> <br>
>     Kamailio services - <a href="https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgilawa.com%2F&amp;data=05%7C01%7Cbkaufman%40bcmone.com%7C233860f64707428040a008da2e090ef7%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C637872913679726971%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=W7RpDwuZE%2BL%2BkFtj98XacFKD6JcnukPdg36I0JyD%2Fv8%3D&amp;reserved=0" rel="noreferrer" target="_blank">https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgilawa.com%2F&amp;data=05%7C01%7Cbkaufman%40bcmone.com%7C233860f64707428040a008da2e090ef7%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C637872913679726971%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=W7RpDwuZE%2BL%2BkFtj98XacFKD6JcnukPdg36I0JyD%2Fv8%3D&amp;reserved=0</a><br>
>     <br>
> <<a href="https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgil" rel="noreferrer" target="_blank">https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgil</a><br>
> <a href="http://awa.com" rel="noreferrer" target="_blank">awa.com</a>%2F&amp;data=05%7C01%7Cbkaufman%<a href="http://40bcmone.com" rel="noreferrer" target="_blank">40bcmone.com</a>%7C233860f647074280<br>
> 40a008da2e090ef7%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C63787291<br>
> 3679726971%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzI<br>
> iLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=W7RpDwuZE%2B<br>
> L%2BkFtj98XacFKD6JcnukPdg36I0JyD%2Fv8%3D&amp;reserved=0><br>
> <br>
>     *From:*sr-users <<a href="mailto:sr-users-bounces@lists.kamailio.org" target="_blank">sr-users-bounces@lists.kamailio.org</a><br>
>     <mailto:<a href="mailto:sr-users-bounces@lists.kamailio.org" target="_blank">sr-users-bounces@lists.kamailio.org</a>>> *On Behalf Of *Joel<br>
>     Serrano<br>
>     *Sent:* Tuesday, May 3, 2022 6:20 PM<br>
>     *To:* Kamailio (SER) - Users Mailing List<br>
>     <<a href="mailto:sr-users@lists.kamailio.org" target="_blank">sr-users@lists.kamailio.org</a> <mailto:<a href="mailto:sr-users@lists.kamailio.org" target="_blank">sr-users@lists.kamailio.org</a>>><br>
>     *Subject:* [SR-Users] Tips on picking random element from a list<br>
> <br>
>     Hi Everyone,<br>
> <br>
>     I have to implement a flow where outbound calls get their caller-id<br>
>     set with a random number from a pool.<br>
> <br>
>     I've been looking around and, as most of the time, there are tons of<br>
>     ways to implement something like this... This is a simple<br>
>     implementation, the number pool is fixed and will not change (so I<br>
>     could have it hard-coded in the config if required).<br>
> <br>
>     My initial approach was:<br>
> <br>
>     1- Try to keep it simple, with no dependencies, and all done in the<br>
>     config script.<br>
> <br>
>     2- If #1 is "ugly", then I'd probably go with some inline code using<br>
>     python/lua/etc<br>
> <br>
>     3- If #2 is "ugly", then I'd go with having the info in a DB and<br>
>     just run a query<br>
> <br>
>     I think #2 and #3 might be overkill for something so "simple" but I<br>
>     haven't had to work too much with randomness in Kamailio, so any<br>
>     tips/suggestions on where to store the number list and how to pick<br>
>     one randomly would be awesome!<br>
> <br>
>     Thanks!!<br>
> <br>
>     Joel.<br>
> <br>
> <br>
> __________________________________________________________<br>
> Kamailio - Users Mailing List - Non Commercial Discussions<br>
>    * <a href="mailto:sr-users@lists.kamailio.org" target="_blank">sr-users@lists.kamailio.org</a><br>
> Important: keep the mailing list in the recipients, do not reply only to the sender!<br>
> Edit mailing list options or unsubscribe:<br>
>    * <br>
> <a href="https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flist" rel="noreferrer" target="_blank">https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flist</a><br>
> <a href="http://s.kamailio.org" rel="noreferrer" target="_blank">s.kamailio.org</a>%2Fcgi-bin%2Fmailman%2Flistinfo%2Fsr-users&amp;data=05%7<br>
> C01%7Cbkaufman%<a href="http://40bcmone.com" rel="noreferrer" target="_blank">40bcmone.com</a>%7C233860f64707428040a008da2e090ef7%7Cafc18<br>
> 18e7b6848568913201b9396c4fc%7C1%7C0%7C637872913679726971%7CUnknown%7CT<br>
> WFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI<br>
> 6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=SxRAZ8yS3dvYYor2Iw8HgG39mvXsn4DP2dSH<br>
> VjM5UO0%3D&amp;reserved=0<br>
<br>
__________________________________________________________<br>
Kamailio - Users Mailing List - Non Commercial Discussions<br>
  * <a href="mailto:sr-users@lists.kamailio.org" target="_blank">sr-users@lists.kamailio.org</a><br>
Important: keep the mailing list in the recipients, do not reply only to the sender!<br>
Edit mailing list options or unsubscribe:<br>
  * <a href="https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.kamailio.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fsr-users&amp;data=05%7C01%7Cbkaufman%40bcmone.com%7C233860f64707428040a008da2e090ef7%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C637872913679726971%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=SxRAZ8yS3dvYYor2Iw8HgG39mvXsn4DP2dSHVjM5UO0%3D&amp;reserved=0" rel="noreferrer" target="_blank">https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.kamailio.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fsr-users&amp;data=05%7C01%7Cbkaufman%40bcmone.com%7C233860f64707428040a008da2e090ef7%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C637872913679726971%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=SxRAZ8yS3dvYYor2Iw8HgG39mvXsn4DP2dSHVjM5UO0%3D&amp;reserved=0</a><br>
<br>
__________________________________________________________<br>
Kamailio - Users Mailing List - Non Commercial Discussions<br>
  * <a href="mailto:sr-users@lists.kamailio.org" target="_blank">sr-users@lists.kamailio.org</a><br>
Important: keep the mailing list in the recipients, do not reply only to the sender!<br>
Edit mailing list options or unsubscribe:<br>
  * <a href="https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users" rel="noreferrer" target="_blank">https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users</a><br>
</blockquote></div>