Python Generate Random Data with Probability

You can use the random module in Python to generate data randomly based on probability. Here are the specific steps:

  1. import the random module.
import random
  1. Define the probability distribution for each data point.
probabilities = [0.2, 0.3, 0.5]  # 分别对应生成数据1、2、3的概率
  1. Generate data based on probability using the random.choices() method.
data = random.choices([1, 2, 3], probabilities, k=10)  # 生成10个数据
print(data)

In the example above, we defined the probabilities corresponding to data 1, 2, and 3 as 0.2, 0.3, and 0.5, and then used the random.choices() method to generate 10 data points. You can modify the code according to your specific requirements and probability distribution.

bannerAds