Wednesday, February 29, 2012

Re: [Rails] ensuring random list of numbers are unique

On 29 February 2012 00:10, John Merlino <stoicism1@aol.com> wrote:
> 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.

This is a classic "how do I achieve my solution?", rather than a
"here's my problem... how can I solve it" post...

Can we back up a second please?
What are you trying to achieve with this list of 500 numbers? And what
do you want to do if the list you have generated does contain
duplicates?
As already said, you can use the .uniq method to remove dupes - but
that might leave you with an array of 496 elements... would that be a
problem? Are you really asking "how do I generate an array of 500
random numbers"?

What do you want to do with these 500 random numbers? Considering that
one user could make a request and get their 500 randoms, and another
user could do the same - there could be duplicates across users. Is
this going to be a problem?

Please try to ask clearer questions, as the way it stands, I could
spend ages of my time coming up with solutions to what I *think* is
your problem, just to have you say "nope... that's not what I was
after". Multiply that across the subscribers to this list, and you've
wasted a hell of a lot of peoples' time around the world :-/


PS BTW Why are you multiplying by 1 in your example code? (and does
that line even work if you paste it into your console, as it explodes
for me with a LocalJumpError... but that may just be Ruby versions...)

--
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate