3
I would like to have the items in an array shuffled. Something like this:
[1,2,3,4].scramble => [2,1,3,4]
[1,2,3,4].scramble => [3,1,2,4]
[1,2,3,4].scramble => [4,2,3,1]
and so on, randomly. How to do?
3
I would like to have the items in an array shuffled. Something like this:
[1,2,3,4].scramble => [2,1,3,4]
[1,2,3,4].scramble => [3,1,2,4]
[1,2,3,4].scramble => [4,2,3,1]
and so on, randomly. How to do?
4
You can use the method sample that you pass the amount you want to return, or still have the shuffle, follows an example:
array = [1,2,3,4]
#shuffle
>> array.shuffle
=> [4, 1, 2, 3]
#sample
>> array.sample(array.length)
=> [3, 1, 2, 4]
>> array.sample(array.length)
=> [2, 4, 1, 3]
I hope I’ve helped :)
Browser other questions tagged ruby random
You are not signed in. Login or sign up in order to post.