英文:
How is arbitrary distributed for Int? Why is it limited by so small values?
问题
我正在尝试比较QuickCheck库和SmallCheck库。在SmallCheck中,我可以通过操纵depth
参数来达到特定值。在QuickCheck中:
>a<-generate (replicateM 10000 arbitrary) :: IO [Int]
>length a
10000
>maximum a
30
然后我的问题是:为什么 10,000 个“随机”(“arbitrary”)整数被限制在 30 内?!我期望在范围 0 到 10,000 内看到更多“广泛”分布的值,也许最大值接近 5,000。
英文:
I am trying to compare the QuickCheck library to the SmallCheck one. In SmallCheck I can reach particular value manipulating depth
parameter. In QuickCheck:
>a<-generate (replicateM 10000 arbitrary) :: IO [Int]
>length a
10000
>maximum a
30
and my question then is: why are 10,000 "random" ("arbitrary") integers limited by 30?! I expected to see more "widely" distributed values within the range 0..10,000, maybe the maximum value close to 5,000.
答案1
得分: 4
1 中的文档包含一个线索:
> 传递给生成器的大小始终为 30
默认情况下,QuickCheck 通过从'easy'或'small'输入开始,以查看是否可以找到具有这些输入的反例。只有在没有问题的小输入中找不到问题时,它才逐渐扩大生成输入的范围。size
值(隐式运行在 QuickCheck 所做的一切中)是控制此行为的值。
当您运行 QuickCheck(例如,使用 quickCheck)时,它会自动增加大小。
您真的不应该直接使用 generate
函数,但如果您这样做,您可以 resize 它:
ghci> b <- generate (replicateM 10000 (resize 60 arbitrary)) :: IO [Int]
ghci> maximum b
60
话虽如此,您应该如何使用 QuickCheck 呢?文档 描述了 quickCheck
以及您可以用来评估属性的众多变体。
就个人而言,我将我的 QuickCheck 属性与 testProperty 单元测试框架集成在一起。您可以在这里看到示例:基于属性的测试不同于分区测试。
英文:
The documentation contains a clue:
> The size passed to the generator is always 30
By default QuickCheck works by starting with 'easy' or 'small' inputs to see if it can find counterexamples with those. Only if it finds no problems with the small inputs does it gradually widen the range of generated input. The size
value (which runs implicitly throughout everything that QuickCheck does) is the value that controls this behaviour.
When you run QuickCheck (e.g. with quickCheck) it automatically increases the size as it goes.
You're not really supposed to use the generate
function directly, but if you do, you can resize it:
ghci> b <- generate (replicateM 10000 (resize 60 arbitrary)) :: IO [Int]
ghci> maximum b
60
That said, how are you supposed to use QuickCheck? The documentation describes quickCheck
along with a multitude of variations you can use to evaluate properties.
Personally, I integrate my QuickCheck properties with a unit testing framework with testProperty. You can see examples here: Property-based testing is not the same as partition testing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论