[Solution]Problem 5: Generate Random Numbers According to Probability [TripleByte]
Medium Difficulty
Problem
This problem was asked by Triplebyte.
You are given n
numbers as well as n
probabilities that sum up to 1. Write a function to generate one of the numbers with its corresponding probability.
For example, given the numbers [1, 2, 3, 4]
and probabilities [0.1, 0.5, 0.2, 0.2]
, your function should return 1
10% of the time, 2
50% of the time, and 3 and…
Keep reading with a 7-day free trial
Subscribe to Technology Made Simple to keep reading this post and get 7 days of free access to the full post archives.