独特的随机数在艾达的间隔列表随机数、间隔、独特、列表

2023-09-11 05:43:15 作者:那信誓旦旦的曾经

我很抱歉打扰你,我知道这个问题已要求相当了很多,但从来没有与阿达......我在想,如果有在阿达标准库的方式来产生独特的随机数列表(你从来没有挑相同数量的两倍)在O(N) 在某种程度上是那里的高德纳 - 费雪耶茨算法的Ada的实现?

I'm sorry to bother you I know this question have been asked quite a lot but never with Ada... I was wondering if there were in the Ada standard library a way to generate a list of unique random numbers (you never pick twice the same number) in O(n) In a way is there an implementation of the Knuth-Fisher-Yates algorithm in Ada?

推荐答案

有实施的 费雪耶茨 洗牌的此处。基本上,你需要一个不同的范围 < $ C C> Discrete_Random $ 在每次迭代,但是的 Float_Random 是一种选择,在提到的 A.5.2(50),注意16 。如果偏压不是关键的,这示例的可能是足够的。

There's a discussion of implementing the Fisher–Yates shuffle here. Basically, you need a different range of Discrete_Random in each iteration, but Float_Random is an alternative, as mentioned in A.5.2(50), Note 16. If bias isn't critical, this example may be sufficient.

在任何情况下,的洗牌的是 O(N)的,但是的选择的可的 O(1)的

In any case, shuffling is O(n), but selecting can be O(1).

附录:在对的创建复杂的设定依赖于的实施。例如, Containers.Hashed_Sets,A。 18.8(二分之八十八)和的 Containers.Ordered_Sets,A.18.9(二分之一百十六)。

Addendum: The complexity of creating the set depends on the implementation. For example, Containers.Hashed_Sets, A.18.8(88/2) and Containers.Ordered_Sets, A.18.9(116/2).