很抱歉打扰您,我知道这个问题已经被问了很多次,但是从来没有使用 Ada...我想知道 Ada 标准库中是否有一种方法可以在 O(n) 中生成唯一随机数列表(你永远不会选择两次相同的数字)
在某种程度上,Ada 中是否有 Knuth-Fisher-Yates 算法的实现?
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?
发布评论
评论(3)
有一个关于实施
Fisher–Yates
在此处随机播放。基本上,您需要不同范围的Discrete_Random
在每次迭代中;Float_Random
是一种替代方案,如 AI12-0144-1。这个示例说明了两种方法:如果偏差不重要,则此处或此处 可能就足够了。如果 Ada 2022 可用,则会说明带有范围参数的新Random
重载 此处。无论如何,洗牌的时间为O(n),但选择的时间可能为O(1)。
附录:创建该集合的复杂性取决于实施。例如,Containers.Hashed_Sets,A .18.8(88/2) 和 Containers.Ordered_Sets,A.18.9(116/2)。
There's a discussion of implementing the
Fisher–Yates
shuffle here. Basically, you need a different range ofDiscrete_Random
in each iteration;Float_Random
is an alternative, as mentioned in AI12-0144-1. This example illustrates two approaches: If bias isn't critical, the approach seen here or here may be sufficient. If Ada 2022 is available, the newRandom
overload with range parameters is illustrated here.In any case, shuffling is O(n), but selecting can be O(1).
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).
鉴于您想要:
a) 0 到 1000 之间的随机数
和
b) 数字不能重复
根据您提供的链接,您可以相当轻松地做到这一点。
只需用值范围填充数组并对其随机选择的元素执行一定次数的交换即可;这保证了这两个要求都得到满足。
Given that you want:
a) Random numbers from 0 to 1000
and
b) the numbers are not to repeat
according to the link you provided, you could do this rather easily.
Just fill an array with the range of values and perform some number of swaps on randomly chosen elements thereof; this guarantees both requirements are upheld.
我冒昧地对其进行了编码。
您需要使用 Ada.Numerics.Discrete_Random。
你可以用类似的东西来测试它:
I took the liberty of coding it up.
You'll need to With Ada.Numerics.Discrete_Random.
And you can test it out with something like: