0

For two given arrays the first for items ( elements) and the second for their probabilities how to select a number based the probability ? in c#

  • 1
    what you tried? – daremachine Apr 24 '22 at 18:53
  • 2
    Can you add sample code and show us an example of input and expected output? – shree.pat18 Apr 24 '22 at 18:53
  • 1
    Does this answer your question? [Pick random element from list with probability](https://stackoverflow.com/questions/46735106/pick-random-element-from-list-with-probability) – Andrew Morton Apr 24 '22 at 18:55
  • How does the arrays look like? and when you say "select a number based the probability" what does that mean? A method where you supply the probability, and you want the corresponding number? or you want to sort the list by probability and take the one with the highest? – Hans-Henrik Møller Apr 24 '22 at 19:41
  • Please provide enough code so others can better understand or reproduce the problem. – Maritim Apr 24 '22 at 21:14

1 Answers1

0
    const int SIZE = 4;//write your size

    var array = new int[SIZE] { 1, 2, 3, 4 };//fill your items
    var probabilities = new double[SIZE] { 43.5, 20, 100, 13.5 };//fill your probabilities

    for (int i = 0; i < probabilities.Length; i++)
    {
        int countOfTempItem = array.Count(x => x == array[i]);
        var probOfArrayTempItem = (double)((countOfTempItem / array.Length) * 100);
        if (probabilities[i] == probOfArrayTempItem)
        {
            Console.WriteLine($"Item: {array[i]} - Probability {probabilities[i]}");
        }
    }

May be this can help you.