如何计算“15%的时间”?随机性?
我正在寻找一种体面、优雅的方法来计算这个简单的逻辑。
现在我想不出一个,它让我头晕目眩。
我只有 15% 的时间需要执行某些操作。
我习惯了“50% 的时间”,我只是修改当前时间的毫秒数,看看它是奇数还是偶数,但我认为这不太优雅。
我如何优雅地计算“15%的时间”?也许是随机数生成器?
欢迎伪代码或任何语言。
希望这不是主观的,因为我正在寻找“最聪明”的速记方法。
谢谢。
I'm looking for a decent, elegant method of calculating this simple logic.
Right now I can't think of one, it's spinning my head.
I am required to do some action only 15% of the time.
I'm used to "50% of the time" where I just mod the milliseconds of the current time and see if it's odd or even, but I don't think that's elegant.
How would I elegantly calculate "15% of the time"? Random number generator maybe?
Pseudo-code or any language are welcome.
Hope this is not subjective, since I'm looking for the "smartest" short-hand method of doing that.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用模算术,您可以轻松地在每次运行 X 次时执行一些操作,例如
if( microtime() % 6 === ) 执行
其他操作:
Using modulo arithmetic you can easily do something every Xth run like so
if( microtime() % 6 === ) do it
other thing:
这是一种结合了随机性和保证最终在可预测范围内获得积极结果的方法:
有一个目标(在您的例子中为 15)、一个计数器(初始化为 0)和一个标志(初始化为 false)。
这意味着第一个请求返回阳性结果的概率为 1/15,但到第 15 个请求时,如果没有返回阳性结果,则返回阳性结果的概率为 1/1。
Here's one approach that combines randomness and a guarantee that eventually you get a positive outcome in a predictable range:
Have a target (15 in your case), a counter (initialized to 0), and a flag (initialized to false).
This means that the first request has probability of 1/15 of return positive, but by the 15th request, if no positive result has been returned, there's a probability of 1/1 of a positive result.
这句话来自一篇关于如何使用随机数生成器的精彩文章:
它包含
This quote is from a great article about how to use a random number generator:
and it contains formulas and pseudo-code for
解决方案 1 (double)
解决方案 2 (int )
您还可以通过创建一个随机 int 并查看它是否可整除 6 或 7 来实现此目的。 更新 -->这不是最佳的。
Solution 1 (double)
Solution 2 (int)
You can also achieve this by creating a random int and see if it is dividable to 6 or 7. UPDATE --> This is not optimal.
您可以生成 0 到 99 之间的随机数,并检查它是否小于 15:
您还可以减少数字,因为 15/100 与 3/20 相同:
You can produce a random number between 0 and 99, and check if it's less than 15:
You can also reduce the numbers, as 15/100 is the same as 3/20:
随机数生成器将为您提供最佳的随机性。生成 0 到 1 之间的随机数,测试 << 0.15。
像这样使用时间并不是真正的随机,因为它受到处理时间的影响。如果一个任务的运行时间少于 1 毫秒,那么下一个随机选择将是相同的。
也就是说,如果您确实想使用基于毫秒的方法,请执行 milliseconds % 20
milliseconds % 20
milliseconds % 20 < 3.
。Random number generator would give you the best randomness. Generate a random between 0 and 1, test for < 0.15.
Using the time like that isn't true random, as it's influenced by processing time. If a task takes less than 1 millisecond to run, then the next random choice will be the same one.
That said, if you do want to use the millisecond-based method, do
milliseconds % 20 < 3
.只需使用 PRNG。与往常一样,这是性能与准确性的权衡。我认为直接在空闲时间自己做是浪费时间(双关语)。您可能会得到比普通线性同余发生器更糟糕的偏置效应。
在Java中,我会使用 nextInt :
或者(大部分)等效地:
有方法可以用其他语言获取随机整数(实际上有多种方法,具体取决于它的准确程度)。
Just use a PRNG. Like always, it's a performance v. accuracy trade-off. I think making your own doing directly off the time is a waste of time (pun intended). You'll probably get biasing effects even worse than a run of the mill linear congruential generator.
In Java, I would use nextInt:
Or (mostly) equivalently:
There are way to get a random integer in other languages (multiple ways actually, depending how accurate it has to be).