如何计算“15%的时间”?随机性?

发布于 2024-10-07 14:21:59 字数 242 浏览 6 评论 0原文

我正在寻找一种体面、优雅的方法来计算这个简单的逻辑。
现在我想不出一个,它让我头晕目眩。

我只有 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 技术交流群。

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

发布评论

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

评论(8

初相遇 2024-10-14 14:22:00

使用模算术,您可以轻松地在每次运行 X 次时执行一些操作,例如

(6 will give you ruthly 15%

if( microtime() % 6 === ) 执行

其他操作:

if(rand(0,1) >= 0.15) do it

Using modulo arithmetic you can easily do something every Xth run like so

(6 will give you ruthly 15%

if( microtime() % 6 === ) do it

other thing:

if(rand(0,1) >= 0.15) do it
海拔太高太耀眼 2024-10-14 14:22:00
boolean array[100] = {true:first 15, false:rest};
shuffle(array);
while(array.size > 0)
{
    // pop first element of the array.
    if(element == true)
       do_action();
    else
       do_something_else();
}
// redo the whole thing again when no elements are left.
boolean array[100] = {true:first 15, false:rest};
shuffle(array);
while(array.size > 0)
{
    // pop first element of the array.
    if(element == true)
       do_action();
    else
       do_something_else();
}
// redo the whole thing again when no elements are left.
枕头说它不想醒 2024-10-14 14:22:00

这是一种结合了随机性和保证最终在可预测范围内获得积极结果的方法:

有一个目标(在您的例子中为 15)、一个计数器(初始化为 0)和一个标志(初始化为 false)。

Accept a request.
If the counter is 15, reset the counter and the flag.
If the flag is true, return negative outcome.
Get a random true or false based on one of the methods described in other answers, but use a probability of 1/(15-counter). 
Increment counter
If result is true, set flag to true and return a positive outcome. Else return a negative outcome.
Accept next request

这意味着第一个请求返回阳性结果的概率为 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).

Accept a request.
If the counter is 15, reset the counter and the flag.
If the flag is true, return negative outcome.
Get a random true or false based on one of the methods described in other answers, but use a probability of 1/(15-counter). 
Increment counter
If result is true, set flag to true and return a positive outcome. Else return a negative outcome.
Accept next request

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.

寂寞笑我太脆弱 2024-10-14 14:22:00

这句话来自一篇关于如何使用随机数生成器的精彩文章

注意:请勿使用

 y = rand() % M;

因为这集中于较低位
兰特()。对于线性同余随机
数字生成器,rand() 经常使用
也就是说,低字节要少得多
比高字节随机。实际上
最低位在 0 和 1 之间循环。
因此 rand() 可能在偶数和偶数之间循环
奇怪(尝试一下)。注意 rand() 不
必须是线性同余
随机数生成器。它是
完全允许它是
没有的更好的东西
这个问题。

它包含

  • r = [0,1) = {r: 0 <= r < 的公式和伪代码1} 实数
  • x = [0,M) = {x: 0 <= x < M} 实数
  • y = [0,M) = {y: 0 <= y < M} 整数
  • z = [1,M] = {z: 1 <= z <= M} 整数

This quote is from a great article about how to use a random number generator:

Note: Do NOT use

  y = rand()  %  M;

as this focuses on the lower bits of
rand(). For linear congruential random
number generators, which rand() often
is, the lower bytes are much less
random than the higher bytes. In fact
the lowest bit cycles between 0 and 1.
Thus rand() may cycle between even and
odd (try it out). Note rand() does not
have to be a linear congruential
random number generator. It's
perfectly permissible for it to be
something better which does not have
this problem.

and it contains formulas and pseudo-code for

  • r = [0,1) = {r: 0 <= r < 1} real
  • x = [0,M) = {x: 0 <= x < M} real
  • y = [0,M) = {y: 0 <= y < M} integer
  • z = [1,M] = {z: 1 <= z <= M} integer
神妖 2024-10-14 14:21:59

解决方案 1 (double)

  • 获取 0 到 1 之间的随机双精度值(无论你使用什么语言,都必须有这样的函数)
  • 仅当它小于 0.15 时才执行操作

解决方案 2 (int )

您还可以通过创建一个随机 int 并查看它是否可整除 6 或 7 来实现此目的。 更新 -->这不是最佳的。

Solution 1 (double)

  • get a random double between 0 and 1 (whatever language you use, there must be such a function)
  • do the action only if it is smaller than 0.15

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.

妄司 2024-10-14 14:21:59

您可以生成 0 到 99 之间的随机数,并检查它是否小于 15:

if (rnd.Next(100) < 15) ...

您还可以减少数字,因为 15/100 与 3/20 相同:

if (rnd.Next(20) < 3) ...

You can produce a random number between 0 and 99, and check if it's less than 15:

if (rnd.Next(100) < 15) ...

You can also reduce the numbers, as 15/100 is the same as 3/20:

if (rnd.Next(20) < 3) ...
以可爱出名 2024-10-14 14:21:59

随机数生成器将为您提供最佳的随机性。生成 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.

回忆躺在深渊里 2024-10-14 14:21:59

只需使用 PRNG。与往常一样,这是性能与准确性的权衡。我认为直接在空闲时间自己做是浪费时间(双关语)。您可能会得到比普通线性同余发生器更糟糕的偏置效应。

在Java中,我会使用 nextInt

myRNG.nextInt(100) < 15

或者(大部分)等效地:

myRNG.nextInt(20) < 3

有方法可以用其他语言获取随机整数(实际上有多种方法,具体取决于它的准确程度)。

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:

myRNG.nextInt(100) < 15

Or (mostly) equivalently:

myRNG.nextInt(20) < 3

There are way to get a random integer in other languages (multiple ways actually, depending how accurate it has to be).

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