Valery Kvon wrote in post #1049372:
> 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.
One simple way to get what you want is by using a set:
Here's your method of generating random numbers. Don't count on it
giving you "secure" random numbers, but this technique won't care how
you produced them.
#!/usr/bin/env ruby
require 'set'
def random_numbers(n = 500)
my_set = Set.new
while my_set.size < 500
value = Integer((rand * 1) * 1000) / Float(1000)
my_set << value
end
my_set.to_a
end
puts random_numbers.size
--
Posted via http://www.ruby-forum.com/.
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment