mardi 21 août 2018

Ruby: using `.each` or `.step`, step forward a random amount for each iteration

(Also open to other similar non-Rails methods)

Given (0..99), return entries that are randomly picked in-order.

Example results:

0, 5, 11, 13, 34..

3, 12, 45, 67, 87

0, 1, 2, 3, 4, 5.. (very unlikely, of course)

Current thought:

(0..99).step(rand(0..99)).each do |subindex|
  array.push(subindex)
end

However, this sets a single random value for all the steps whereas I'm looking for each step to be random.




Aucun commentaire:

Enregistrer un commentaire