带有指定概率随机过滤字符串的凹槽表达式
输入是一个随机字符串(不均匀分布)。
我需要一个单线凹槽的表达式,基于输入,具有指定的概率,使我返回真或错误。
对于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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这起作用正确:
This works just right: