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

Fred Posner fred at palner.com
Wed May 4 22:01:53 CEST 2022


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. 
> http://www.kamailio.org/docs/modules/5.6.x/modules/cfgutils.html#cfgutils.f.core_hash 
> <http://www.kamailio.org/docs/modules/5.6.x/modules/cfgutils.html#cfgutils.f.core_hash>
> 
> 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://kamailio.org/docs/modules/5.5.x/modules/cfgutils.html#idm438
>     <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%7Cde7dd7f8eb504d2a662f08da2d6f2f33%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C637872252819501517%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=JRp6T14RXqTW26xFJ5DWcKPUEqqXy0hkQ1ecuCWyk10%3D&reserved=0>
> 
>     Cheers,
> 
>     Henning
> 
>     -- 
> 
>     Henning Westerholt – https://skalatan.de/blog/
>     <https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fskalatan.de%2Fblog%2F&data=05%7C01%7Cbkaufman%40bcmone.com%7Cde7dd7f8eb504d2a662f08da2d6f2f33%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C637872252819501517%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=l0%2FQ%2ByWZlVi4ZI5mxOrwD7153fNcP0%2FNmXXrd0YLg4g%3D&reserved=0>
> 
>     Kamailio services – https://gilawa.com
>     <https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgilawa.com%2F&data=05%7C01%7Cbkaufman%40bcmone.com%7Cde7dd7f8eb504d2a662f08da2d6f2f33%7Cafc1818e7b6848568913201b9396c4fc%7C1%7C0%7C637872252819501517%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=mgnKGkPniboT1FOSW7rC84SWWhJrNer3GlvlzZ72%2FDI%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://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users



More information about the sr-users mailing list