Skip to content

Instantly share code, notes, and snippets.

@willf
Created January 18, 2012 00:16
Show Gist options
  • Save willf/1629957 to your computer and use it in GitHub Desktop.
Save willf/1629957 to your computer and use it in GitHub Desktop.
Split into random slices from an array in Ruby
class Array
def split_random(n)
def td(n)
[take(n),drop(n)]
end
def split_random_acc(n,l, acc)
head,tail = l.td(rand(n)+1)
return (acc + [head]) if tail==[]
split_random_acc(n, tail, acc + [head])
end
split_random_acc(n,self,[])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment