On Tue, Feb 28, 2012 at 8:03 PM, Valery Kvon <adda...@gmail.com> wrote: > On 29.02.2012, at 4:10, John Merlino wrote: > >> Hey all, using ruby 1.8.7, I wrote this: >> >> 500.times.map { Integer((rand * 1) * 1000) / Float(1000) } >> >> Basically, this gives me a list of 500 random decimal numbers that are >> rounded to 3 places. However, I also want to make sure that all are >> unique. > > .uniq() doesn't guarentee 500 number exactly. > .shuffle() just tosses the collection and doesn't give you unique numbers. > > You'd better to implement method: > > def secure(number, precision) > collection = [] > collection |= [SecureRandom.random_number.round(precision)] while > collection.size < number > collection > end > >> secure(500, 3) > => collection of 500 unique random numbers. >
Sweet! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.