Skip to content

Instantly share code, notes, and snippets.

View frederickayala's full-sized avatar

Frederick Ayala frederickayala

  • Eötvös Loránd University
  • Barcelona, Spain
  • X @fredayala
View GitHub Profile

Keybase proof

I hereby claim:

  • I am frederickayala on github.
  • I am frederickayala (https://keybase.io/frederickayala) on keybase.
  • I have a public key ASD_UjlS7JgSlMmZUtm2CuVHrT_cwH3I05Z-jS6Rbd3Cjgo

To claim this, I am signing this object:

def takeRandomWithWeight[T](from: scala.collection.immutable.Vector[(T,Double)], items: scala.collection.immutable.Vector[(T,Double)], remaining: Int, rand: Random): scala.collection.immutable.Vector[(T,Double)] = {
if (remaining == 0) items
else {
val r = rand.nextDouble()
val candidates = from.dropWhile{_._2 >= r}
if(!candidates.isEmpty){
takeRandomWithWeight(from.filterNot(candidates.head == _), candidates.head +: items, remaining -1,rand)
}
else{
takeRandomWithWeight(from,items,remaining,rand)