0

In Python 3.x, I need to choose an integer at random among the indices of a given list. In other words, given

mylist = [0, 5, 6, 8, -10] 

I want to return a number between 0 and 4. What's the most Pythonic way to do it? I tried

import numpy as np my_list = [0, 5, 6, 8, -10] def choose_at_random(a_list): choice = np.random.randint(0, len(a_list)) return choice 

This works, but is this the Pythonic way to do it?

3
  • 1
    random.randint is inclusive in both ends. Commented Aug 23, 2020 at 9:07
  • @Asocia so I need to remove the +1, right? Commented Aug 23, 2020 at 9:09
  • 2
    I don't know about the np.random.randint but for builtin random.randint you don't need the +1 at the end. Commented Aug 23, 2020 at 9:10

2 Answers 2

1

If you just want to use something from Python's standard library (and don't need anything vectorised like numpy) randrange is generally the easiest method for accomplishing this.

You'd use it something like:

from random import randrange from typing import Sized def random_index_from_sized(a_list: Sized) -> int: return randrange(len(a_list)) my_list = [0, 5, 6, 8, -10] random_index_from_sized(my_list) 

which would return an integer value in [0, 4].

numpy's randint is similarly defined, so could be used in the above definition as:

from numpy.random import randint def random_sized_index(a_list: Sized) -> int: return randint(len(a_list)) 

Returning a single value from numpy is kind of pointless, i.e. numpy is designed for returning large arrays. A quick timeit test says that randrange(5) takes ~0.3µs while randint(5) takes ~2µs (for a single value). If you want, e.g., 1000 values then [randrange(5) for _ in range(1000)] takes ~300µs, while randint(5, size=1000) only takes ~20µs.

Sign up to request clarification or add additional context in comments.

Comments

0
import random #random is in the standard library(so no need to "pip") my_list = [0, 5, 6, 8, -10] def choose_at_random(a_list): choice = (random.choice(a_list)) return choice 

5 Comments

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
as per Mark's comment, this doesn't explain what's going on and, worse, it does the wrong thing. OP seems to mostly want indices into my_list (mostly, because they also seem to want an extra out-of-bounds index at the end as well). you just return uniform draws from the list
@SamMason you're right that qwerty's code is doing the wrong thing. But I (OP) also shouldn't post question when my brain is in vacation mode :-D because what I really wanted is indices from my_list. Thanks for making me notice my mistake. The question is now fixed.
@DeltaIV was thinking of adding a comment to your question asking whether you actually meant what you were asking for, maybe I should have! :)
@qwerty I can't remove my downvote until your answer is edited...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.