September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,31 @@
// 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())
}