September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
31
Task/Knuths-algorithm-S/Kotlin/knuths-algorithm-s.kotlin
Normal file
31
Task/Knuths-algorithm-S/Kotlin/knuths-algorithm-s.kotlin
Normal 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())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue