random.seed(a=None, version=2):
random.seed(42) # Use an integer seed
random.seed("hello") # Use a string seed
random.seed() # Seed from current time or OS sources
a: The seed value. Can be any hashable object. If None, it seeds from the current time or OS-specific randomness sources.version: Specifies how the seed is converted to an integer. Version 2 is the default and recommended.None.random.getstate():
current_state = random.getstate()
setstate() to restore the state.random.setstate(state):
# Assume 'saved_state' holds a state obtained from getstate()
random.setstate(saved_state)
state: The state object obtained from a previous call to getstate().None.random.choices(population, weights=None, cum_weights=None, k=1):
# Select 5 items with replacement
selection = random.choices([1, 2, 3, 4, 5], k=5)
# Select with weights (e.g., 1 is twice as likely as 2)
weighted_selection = random.choices(['A', 'B'], weights=[2, 1], k=10)
population: A sequence or set to sample from.weights: (Optional) A list of weights corresponding to the population elements.cum_weights: (Optional) A list of cumulative weights. Cannot be used with weights.k: (Optional) The number of elements to choose (default is 1).