For example, if I have 100 distinguishable dogs and I want to randomly pick 3 of them. With a pocket-size calculator, I would do 100C3 or something similar. How can I do this in Ruby and Python?
Edition 1: My questions were solved from Razvans and Riccardo Bucco solutions. (I flipped a fair coin once to decide to whom I give a check mark and to whom I give an upvote).
Thank you, everyone.
You would do this in python:
from math import comb
n_combinations = comb(100, 3)
Similarly, for permutations:
from math import perm
n_permutations = perm(100, 3)
perm
and comb
can be used only with python > 3.8. For older versions of python please use these functions:
from math import factorial
def comb(n, k):
return factorial(n) // factorial(k) // factorial(n - k)
def perm(n, k=None):
return factorial(n) // factorial(n - (k or n))