Python Forum
random.choices and random.sample
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random.choices and random.sample
#1
What is the difference between random.choices and random.sample?
Reply
#2
sample() function gives us a specified number of distinct results whereas the choice() function gives us a single value out of the given sequence.

From here
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Read the documentation. I think the docs.python.org documentation is very good.

https://docs.python.org/3/library/random.html

Quote:random.choices(population, weights=None, *, cum_weights=None, k=1)
Return a k sized list of elements chosen from the population with replacement. If the population is empty, raises IndexError.

Quote:random.sample(population, k, *, counts=None)
Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.

Once an element is selected by random.sample() it cannot be selected again. random.choices() can select the same element multiple times. A side effect of this is that random.choices() can return a list that is larger than population (k > len(population)), but random.sample() will raise an exception.
Reply
#4
random.choices() was added in Python 3.6 because of a request to add a weights parameter in the random.choice() function. Read What's new in Python 3.6. random.sample() does not have a weights parameter.

Read also the discussion in bpo-18844
Reply
#5
When I use
import random
numberList = [11, 19, 29, 21, 21]
print(random.sample(numberList, k=3))
I get an ouput of
Output:
[21, 11, 21]
While I was expecting unique values?
Yoriz write Sep-17-2022, 07:08 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#6
21 appears twice in the population. There are two elements in the population that just happen to have the same value. random.choices() does not eliminate duplicates, it prevents selecting the same element more than once.

If you want to eliminate duplicates in the population you can use set()
azizrasul likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Frog Puzzle Random To Custom MoreMoney 4 494 Mar-26-2024, 08:38 AM
Last Post: MoreMoney
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 418 Feb-15-2024, 11:15 AM
Last Post: rawatg
  Create Choices from .ods file columns cspower 3 622 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  random numbers, randint janeik 2 577 Nov-27-2023, 05:17 PM
Last Post: janeik
  Sample random, unique string pairs from a list without repetitions walterwhite 1 465 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 521 Oct-24-2023, 05:56 AM
Last Post: buran
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,260 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 2,725 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,531 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  random methods Skaperen 16 2,834 Sep-28-2022, 10:44 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020