Sampling with replacement, specify the probabilities
Scala:

def weightedSampleWithReplacement[T](data: Array[T], 
                                     weights: Array[Double], 
                                     n: Int, 
                                     random: Random): Array[T] = {
  val cumWeights = weights.scanLeft(0.0)(_ + _)
  val cumProbs = cumWeights.map(_ / cumWeights.last)
  Array.fill(n) {
    val r = random.nextDouble()
    data(cumProbs.indexWhere(r &lt; _) - 1)
  }
} 


Spark has an RDD.sample() method that can sample without replacement, though not with weights. You could probably adapt that method along the lines above to do this however.