Module Random::ArrayExtensions
In: lib/facets/standard/facets/random.rb

Methods

at_rand   at_rand!   pick   pick!   rand_index   rand_subset   shuffle   shuffle!  

Public Instance methods

Return a random element from the array.

  [1, 2, 3, 4].at_rand           #~> 2
  [1, 2, 3, 4].at_rand           #~> 4

[Source]

# File lib/facets/standard/facets/random.rb, line 128
    def at_rand
      at(Random.number(size))
    end

Same as at_rand, but acts in place removing a random element from the array.

  a = [1,2,3,4]
  a.at_rand!       #~> 2
  a                #~> [1,3,4]

[Source]

# File lib/facets/standard/facets/random.rb, line 139
    def at_rand!
      return delete_at( Random.number( size ) )
    end

Similar to at_rand, but will return an array of randomly picked exclusive elements if given a number.

[Source]

# File lib/facets/standard/facets/random.rb, line 145
    def pick(n=nil)
      if n
        a = self.dup
        a.pick!(n)
      else
        at(Random.number(size))
      end
    end

Similar to at_rand!, but given a number will return an array of exclusive elements.

[Source]

# File lib/facets/standard/facets/random.rb, line 156
    def pick!(n=nil)
      if n
        if n > self.size
          r = self.dup
          self.replace([])
          r
        else
          r = []
          n.times { r << delete_at(Random.number(size)) }
          r
        end
      else
        delete_at(Random.number(size))
      end
    end

Random index.

[Source]

# File lib/facets/standard/facets/random.rb, line 174
    def rand_index
      Random.number(size)
    end

Returns a random subset of an Array. If a number of elements is specified then returns that number of elements, otherwise returns a random number of elements upto the size of the Array.

By defualt the returned values are exclusive of each other, but if exclusive is set to false, the same values can be choosen more than once.

When exclusive is true (the default) and the number given is greater than the size of the array, then all values are returned.

  [1, 2, 3, 4].rand_subset(1)        #~> [2]
  [1, 2, 3, 4].rand_subset(4)        #~> [2, 1, 3, 4]
  [1, 2, 3, 4].rand_subset           #~> [1, 3, 4]
  [1, 2, 3, 4].rand_subset           #~> [2, 3]

[Source]

# File lib/facets/standard/facets/random.rb, line 196
    def rand_subset( number=nil, exclusive=true )
      number = Random.number(size) unless number
      number = number.to_int
      #return self.dup if (number >= size and exlusive)
      return sort_by{rand}.slice(0,number) if exclusive
      ri =[]; number.times { |n| ri << Random.number(size) }
      return values_at(*ri)
    end

Randomize the order of an array.

  [1,2,3,4].shuffle  #~> [2,4,1,3]

[Source]

# File lib/facets/standard/facets/random.rb, line 209
    def shuffle
      dup.shuffle!
      #sort_by{Random.number}
    end

As with shuffle but modifies the array in place. The algorithm used here is known as a Fisher-Yates shuffle.

  a = [1,2,3,4]
  a.shuffle!

  a  #~> [2,4,1,3]

CREDIT Niel Spring

[Source]

# File lib/facets/standard/facets/random.rb, line 223
    def shuffle!
      s = size
      each_index do |j|
        i = Random.number(s-j)
        #self[j], self[j+i] = self[j+i], self[j]
        tmp = self[j]
        self[j] = self[j+i]
        self[j+i] = tmp
      end
      self
    end

[Validate]