如何生成满足一些限制的整数? [英] How can I generate integers that satisfy some restrictions?

查看:157
本文介绍了如何生成满足一些限制的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,我需要生成整数x和y,使得

p>

  100> x 
和y < x + 5

我不是指这个特殊的例子,而是一些生成整数的通用技术,


b
  • 检查您的条件

  • 如果一个条件失败,请返回步骤1.

  • 如果您的示例中有多个整数,例如 x y ,将整数替换为整数。



    此技术也称为拒绝采样



    例如,你可以用一系列链接的迭代器来实现。和一些约束在发电机工作得很好,例如正整数小于100,所以你可能开始与其中一个之前过滤所有其他约束。



    我会看到的唯一的其他选择适用于一般的限制是分析你的约束和生成数字,而不是猜测,但知道如何生成它们。这对于诸如0< x < 100,但超出了它实现计算机代数系统的紧密关系。还要记住,你必须同时满足每个约束...什么是拒绝抽样长期将使这种方法成为一个噩梦实施。


    Can anyone give me a hand with techniques to generate integers that satisfy certain restrictions.

    For example, say I need to generate integers x and y such that

          100 > x
    and   y < x + 5
    

    And I don't mean this particular example but some generic techniques to generate integers that satisfy certain conditions.

    解决方案

    Well, that's not that hard:

    1. Pick an integer, mayhaps randomly.
    2. Check your conditions
    3. If one condition fails, back to step 1.

    If you have multiple integers, such as x and y in your example, replace "an integer" by "integers".

    This technique is also known as rejection sampling.

    You could implement this with a series of chained iterators, for example. And some constraints work pretty well as generators, such as "positive integers less than 100", so you'd probably start out with one of those before filtering through all other constraints.

    The only other option I'd see that applies to general restrictions would be to analyze your constraints and generate numbers without guessing but knowing how to generate them. This is trivial for constraints such as "0 < x < 100" but beyond that it borders closely on implementing a computer algebra system. Also keep in mind that you have to simultaneously satisfy every constraint ... what takes rejection sampling long will make this approach a nightmare to implement.

    这篇关于如何生成满足一些限制的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆