I would also prefer a `random.shuffled` function. The reason I didn't propose it is because there's usually more resistance for adding new functions. But in my view that'll be the best solution.

On Sat, Aug 1, 2020 at 9:17 PM Alex Hall <alex.mojaki@gmail.com> wrote:
I agree that calling random.shuffle imperatively is annoying. But I don't think your proposed solution is readable. You're not taking a sample. A sample generally implies a strict subset, usually quite a small one.

I've often thought there should just be a `random.shuffled()` function which returns a shuffled copy, similar to `.sort()` and `sorted()` or `.reverse()` and `reversed()`.

On Sat, Aug 1, 2020 at 7:59 PM Ram Rachum <ram@rachum.com> wrote:
When writing some code now, I needed to produce a shuffled version of `range(10, 10 ** 5)`.

This is one way to do it: 

shuffled_numbers = list(range(10, 10 ** 5))
random.shuffle(shuffled_numbers)

I don't like it because (1) it's too imperative and (2) I'm calling the list "shuffled" even before it's shuffled.

Another solution is this: 

shuffled_numbers = random.sample(range(10, 10 ** 5), k=len(range(10, 10 ** 5)))

This is better because it solves the 2 points above. However, it is quite cumbersome.

I notice that the `random.sample` function doesn't have a default behavior set when you don't specify `k`. This is fortunate, because we could make that behavior just automatically take the length of the first argument. So we could do this: 

shuffled_numbers = random.sample(range(10, 10 ** 5))

What do you think? 


Thanks,
Ram.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/OHLXVKIBMNSQO6BCFK6LEHSYNXDB6OQJ/
Code of Conduct: http://python.org/psf/codeofconduct/