mardi 7 août 2018

How to create an array which has three 0s and two 1s in a random permutation in ruby?

Wanna create a ruby array which has 5 numbers consist of three 0s and two 1s in a random permutation

such as, [ 0, 1, 0, 0, 1 ] or [ 1, 1, 0, 0, 0]

I know that

 Array.new(5) { rand(2) } 

could create five random numbers consist of 0 and 1

but how can I make sure there are exactly three 0s and two 1s ?




Aucun commentaire:

Enregistrer un commentaire