[SR-Users] Tips on picking random element from a list

Joel Serrano joel at textplus.com
Thu May 5 19:39:51 CEST 2022


Hey Ben,

Something like that is exactly what I was looking for, and you just gave me
the headstart I needed! Appreciate the snippet!

Best,
Joel.

On Wed, May 4, 2022 at 4:06 PM Ben Kaufman <bkaufman at bcmone.com> wrote:

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


More information about the sr-users mailing list