Knuth's algorithm S: Difference between revisions
Content added Content deleted
(Added Kotlin) |
|||
Line 940: | Line 940: | ||
8 => 30147 |
8 => 30147 |
||
9 => 30053 |
9 => 30053 |
||
</pre> |
|||
=={{header|Kotlin}}== |
|||
{{trans|Java}} |
|||
<lang scala>// version 1.1.2 |
|||
import java.util.Random |
|||
class SOfN<T>(val n: Int) { |
|||
private val sample = ArrayList<T>(n) |
|||
private var i = 0 |
|||
private companion object { |
|||
val rand = Random() |
|||
} |
|||
fun process(item: T): ArrayList<T> { |
|||
if (++i <= n) |
|||
sample.add(item) |
|||
else if (rand.nextInt(i) < n) |
|||
sample[rand.nextInt(n)] = item |
|||
return sample |
|||
} |
|||
} |
|||
fun main(args: Array<String>) { |
|||
val bin = IntArray(10) |
|||
(1..100_000).forEach { |
|||
val sOfn = SOfN<Int>(3) |
|||
var sample: ArrayList<Int>? = null |
|||
for (i in 0..9) sample = sOfn.process(i) |
|||
for (s in sample!!) bin[s]++ |
|||
} |
|||
println(bin.contentToString()) |
|||
}</lang> |
|||
Sample output: |
|||
{{out}} |
|||
<pre> |
|||
[30144, 29928, 30012, 29938, 30040, 30244, 30107, 29969, 29802, 29816] |
|||
</pre> |
</pre> |
||