带有指定概率随机过滤字符串的凹槽表达式

发布于 2025-02-03 08:05:12 字数 344 浏览 3 评论 0原文

输入是一个随机字符串(不均匀分布)。

我需要一个单线凹槽的表达式,基于输入,具有指定的概率,使我返回真或错误。

对于INT值的输入,我将使用intvalue%100< p其中p是所需的概率百分比。

我该如何为字符串这样做?我正在考虑使用哈希功能,例如md5()使我的输入更加统一。但是groovy中的string.md5()的输出仍然是一个字符串,那么我该怎么办?

我想过比较上一个字符,例如['0','1']。包含(mystring.md5()[31]),但它不允许十进制粒度,并且很麻烦更新。

The input is a random string (not uniformly distributed).

I need a one-liner groovy expression that, based on the input, with specified probability, returns me true or false.

For an input of int value i would use intValue % 100 < p where p is desired probability percentage.

How do I do this for a string? I am thinking of using a hash function, e.g. md5() to make my input more uniform. But the output of string.md5() in groovy is still a string, so what can I do with it?

I thought of comparing last character, e.g. ['0','1'].contains(myString.md5()[31]) but it doesn't allow for decimal granularity and is quite cumbersome to update.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情泪▽动烟 2025-02-10 08:05:12

这起作用正确:

new BigInteger(myString.md5(), 16) % 100 < p

This works just right:

new BigInteger(myString.md5(), 16) % 100 < p
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文