1

I'm trying to produce something close to the Fisher-Yates shuffle for randomness. I know underscore.js uses this technique on the _.shuffle method. In Rails, I use either

.sample

And sometimes I do it this way as its a little faster..

.order("RANDOM()").first

But just how random are these? Is there a better way to make this random?

Trip
  • 26,756
  • 46
  • 158
  • 277
  • 1
    Have you tried ruby's shuffle method? AFAIK it uses the Fisher–Yates shuffle. Also you might find this answer relevant if speed is your concern http://stackoverflow.com/questions/3641057/rails-select-random-record/3641112#3641112 should be faster than `.order("RANDOM()")`. – Noz May 31 '13 at 19:13

1 Answers1

0

i think you can try this

prng = Random.new

prng.rand(100)       # => 42

Read about ruby's Random class.

http://ruby-doc.org/core-2.0/Random.html#method-c-rand

Sachin Singh
  • 7,107
  • 6
  • 40
  • 80
  • Is that a better quality random though? Seems the same as the others. Noting that it doesn't seem to borrow from a Fisher-Yates shuffle. – Trip May 31 '13 at 19:11