def take_sample(with_replacement, num, seed=nil)

      if num < 0
        raise Spark::RDDError, 'Size have to be greater than 0'
      elsif num == 0
        return []
      end

      # Taken from scala
      num_st_dev = 10.0

      # Number of items
      initial_count = self.count
      return [] if initial_count == 0

      # Create new generator
      seed ||= Random.new_seed
      rng = Random.new(seed)

      # Shuffle elements if requested num if greater than array size
      if !with_replacement && num >= initial_count
        return self.shuffle(seed).collect
      end

      # Max num
      max_sample_size = Integer::MAX - (num_st_dev * Math.sqrt(Integer::MAX)).to_i
      if num > max_sample_size
        raise Spark::RDDError, "Size can not be greate than #{max_sample_size}"
      end

      # Approximate fraction with tolerance
      fraction = compute_fraction(num, initial_count, with_replacement)

      # Compute first samled subset
      samples = self.sample(with_replacement, fraction, seed).collect

      # If the first sample didn't turn out large enough, keep trying to take samples;
      # this shouldn't happen often because we use a big multiplier for their initial size.
      index = 0
      while samples.size < num
        log_warning("Needed to re-sample due to insufficient sample size. Repeat #{index}")
        samples = self.sample(with_replacement, fraction, rng.rand(0..Integer::MAX)).collect
        index += 1
      end

      samples.shuffle!(random: rng)
      samples[0, num]
    end
def take_sample(with_replacement, num, seed=nil)

      if num < 0
        raise Spark::RDDError, 'Size have to be greater than 0'
      elsif num == 0
        return []
      end

      # Taken from scala
      num_st_dev = 10.0

      # Number of items
      initial_count = self.count
      return [] if initial_count == 0

      # Create new generator
      seed ||= Random.new_seed
      rng = Random.new(seed)

      # Shuffle elements if requested num if greater than array size
      if !with_replacement && num >= initial_count
        return self.shuffle(seed).collect
      end

      # Max num
      max_sample_size = Integer::MAX - (num_st_dev * Math.sqrt(Integer::MAX)).to_i
      if num > max_sample_size
        raise Spark::RDDError, "Size can not be greate than #{max_sample_size}"
      end

      # Approximate fraction with tolerance
      fraction = compute_fraction(num, initial_count, with_replacement)

      # Compute first samled subset
      samples = self.sample(with_replacement, fraction, seed).collect

      # If the first sample didn't turn out large enough, keep trying to take samples;
      # this shouldn't happen often because we use a big multiplier for their initial size.
      index = 0
      while samples.size < num
        log_warning("Needed to re-sample due to insufficient sample size. Repeat #{index}")
        samples = self.sample(with_replacement, fraction, rng.rand(0..Integer::MAX)).collect
        index += 1
      end

      samples.shuffle!(random: rng)
      samples[0, num]
    end
Return a fixed-size sampled subset of this RDD in an array

 == Examples:
   rdd = $sc.parallelize(0..100)

   rdd.take_sample(true, 10)
   # => [90, 84, 74, 44, 27, 22, 72, 96, 80, 54]

   rdd.take_sample(false, 10)
   # => [5, 35, 30, 48, 22, 33, 40, 75, 42, 32]