Jarrett (jarrett) wrote,

Programming Question of the Day

Okay, sorry I haven't updated lately. I spent a few days in Southshore Lake Tahoe. It was fantastic. Pics to come shortly.

Anyways, I had a programming task and I'm not sure I've discovered the most elegant solution, so I thought I'd unleash it on my readership (Shaun? Ryan?).

Given an array ary() with a upper boundary of u, how would you return all the terms in the array exactly once in random order?

Here's the best I can come up with:

for i = u to 0 step -1
    r = int(rnd * i)
    return ary(r)
    ary(r) = ary(i)

Actually, that doesn't look too bad. That would probably work, wouldn't it? Is there a better way?
  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.