Distributed probability random number generator

前端 未结 7 1024
挽巷
挽巷 2020-12-05 10:47

I want to generate a number based on a distributed probability. For example, just say there are the following occurences of each numbers:

Number| Count               


        
相关标签:
7条回答
  • 2020-12-05 11:00

    Do this only once:

    • Write a function that calculates a cdf array given a pdf array. In your example pdf array is [150,40,15,3], cdf array will be [150,190,205,208].

    Do this every time:

    • Get a random number in [0,1) , multiply with 208, truncate up (or down: I leave it to you to think about the corner cases) You'll have an integer in 1..208. Name it r.
    • Perform a binary search on cdf array for r. Return the index of the cell that contains r.

    The running time will be proportional to log of the size of the given pdf array. Which is good. However, if your array size will always be so small (4 in your example) then performing a linear search is easier and also will perform better.

    0 讨论(0)
  • 2020-12-05 11:02

    I know this is an old post, but I also searched for such a generator and was not satisfied with the solutions I found. So I wrote my own and want to share it to the world.

    Just call "Add(...)" some times before you call "NextItem(...)"

    /// <summary> A class that will return one of the given items with a specified possibility. </summary>
    /// <typeparam name="T"> The type to return. </typeparam>
    /// <example> If the generator has only one item, it will always return that item. 
    /// If there are two items with possibilities of 0.4 and 0.6 (you could also use 4 and 6 or 2 and 3) 
    /// it will return the first item 4 times out of ten, the second item 6 times out of ten. </example>
    public class RandomNumberGenerator<T>
    {
        private List<Tuple<double, T>> _items = new List<Tuple<double, T>>();
        private Random _random = new Random();
    
        /// <summary>
        /// All items possibilities sum.
        /// </summary>
        private double _totalPossibility = 0;
    
        /// <summary>
        /// Adds a new item to return.
        /// </summary>
        /// <param name="possibility"> The possibility to return this item. Is relative to the other possibilites passed in. </param>
        /// <param name="item"> The item to return. </param>
        public void Add(double possibility, T item)
        {
            _items.Add(new Tuple<double, T>(possibility, item));
            _totalPossibility += possibility;
        }
    
        /// <summary>
        /// Returns a random item from the list with the specified relative possibility.
        /// </summary>
        /// <exception cref="InvalidOperationException"> If there are no items to return from. </exception>
        public T NextItem()
        {
            var rand = _random.NextDouble() * _totalPossibility;
            double value = 0;
            foreach (var item in _items)
            {
                value += item.Item1;
                if (rand <= value)
                    return item.Item2;
            }
            return _items.Last().Item2; // Should never happen
        }
    }
    
    0 讨论(0)
  • 2020-12-05 11:05

    Here's an implementation using the Inverse distribution function:

    using System;
    using System.Linq;
    
        // ...
        private static readonly Random RandomGenerator = new Random();
    
        private int GetDistributedRandomNumber()
        {
            double totalCount = 208;
    
            var number1Prob = 150 / totalCount;
            var number2Prob = (150 + 40) / totalCount;
            var number3Prob = (150 + 40 + 15) / totalCount;
    
            var randomNumber = RandomGenerator.NextDouble();
    
            int selectedNumber;
    
            if (randomNumber < number1Prob)
            {
                selectedNumber = 1;
            }
            else if (randomNumber >= number1Prob && randomNumber < number2Prob)
            {
                selectedNumber = 2;
            }
            else if (randomNumber >= number2Prob && randomNumber < number3Prob)
            {
                selectedNumber = 3;
            }
            else
            {
                selectedNumber = 4;
            }
    
            return selectedNumber;
        }
    

    An example to verify the random distribution:

            int totalNumber1Count = 0;
            int totalNumber2Count = 0;
            int totalNumber3Count = 0;
            int totalNumber4Count = 0;
    
            int testTotalCount = 100;
    
            foreach (var unused in Enumerable.Range(1, testTotalCount))
            {
                int selectedNumber = GetDistributedRandomNumber();
    
                Console.WriteLine($"selected number is {selectedNumber}");
    
                if (selectedNumber == 1)
                {
                    totalNumber1Count += 1;
                }
    
                if (selectedNumber == 2)
                {
                    totalNumber2Count += 1;
                }
    
                if (selectedNumber == 3)
                {
                    totalNumber3Count += 1;
                }
    
                if (selectedNumber == 4)
                {
                    totalNumber4Count += 1;
                }
            }
    
            Console.WriteLine("");
            Console.WriteLine($"number 1 -> total selected count is {totalNumber1Count} ({100 * (totalNumber1Count / (double) testTotalCount):0.0} %) ");
            Console.WriteLine($"number 2 -> total selected count is {totalNumber2Count} ({100 * (totalNumber2Count / (double) testTotalCount):0.0} %) ");
            Console.WriteLine($"number 3 -> total selected count is {totalNumber3Count} ({100 * (totalNumber3Count / (double) testTotalCount):0.0} %) ");
            Console.WriteLine($"number 4 -> total selected count is {totalNumber4Count} ({100 * (totalNumber4Count / (double) testTotalCount):0.0} %) ");
    

    Example output:

    selected number is 1
    selected number is 1
    selected number is 1
    selected number is 1
    selected number is 2
    selected number is 1
    ...
    selected number is 2
    selected number is 3
    selected number is 1
    selected number is 1
    selected number is 1
    selected number is 1
    selected number is 1
    
    number 1 -> total selected count is 71 (71.0 %) 
    number 2 -> total selected count is 20 (20.0 %) 
    number 3 -> total selected count is 8 (8.0 %) 
    number 4 -> total selected count is 1 (1.0 %)
    
    0 讨论(0)
  • 2020-12-05 11:06

    Thanks for all your solutions guys! Much appreciated!

    @Menjaraz I tried implementing your solution as it looks very resource friendly, however had some difficulty with the syntax.

    So for now, I just transformed my summary into a flat list of values using LINQ SelectMany() and Enumerable.Repeat().

    public class InventoryItemQuantityRandomGenerator
    {
        private readonly Random _random;
        private readonly IQueryable<int> _quantities;
    
        public InventoryItemQuantityRandomGenerator(IRepository database, int max)
        {
            _quantities = database.AsQueryable<ReceiptItem>()
                .Where(x => x.Quantity <= max)
                .GroupBy(x => x.Quantity)
                .Select(x => new
                                 {
                                     Quantity = x.Key,
                                     Count = x.Count()
                                 })
                .SelectMany(x => Enumerable.Repeat(x.Quantity, x.Count));
    
            _random = new Random();
        }
    
        public int Next()
        {
            return _quantities.ElementAt(_random.Next(0, _quantities.Count() - 1));
        }
    }
    
    0 讨论(0)
  • 2020-12-05 11:13

    Use my method. It is simple and easy-to-understand. I don't count portion in range 0...1, i just use "Probabilityp Pool" (sounds cool, yeah?)

    At circle diagram you can see weight of every element in pool

    Here you can see an implementing of accumulative probability for roulette

    `
    
    // Some c`lass or struct for represent items you want to roulette
    public class Item
    {
        public string name; // not only string, any type of data
        public int chance;  // chance of getting this Item
    }
    
    public class ProportionalWheelSelection
    {
        public static Random rnd = new Random();
    
        // Static method for using from anywhere. You can make its overload for accepting not only List, but arrays also: 
        // public static Item SelectItem (Item[] items)...
        public static Item SelectItem(List<Item> items)
        {
            // Calculate the summa of all portions.
            int poolSize = 0;
            for (int i = 0; i < items.Count; i++)
            {
                poolSize += items[i].chance;
            }
    
            // Get a random integer from 0 to PoolSize.
            int randomNumber = rnd.Next(0, poolSize) + 1;
    
            // Detect the item, which corresponds to current random number.
            int accumulatedProbability = 0;
            for (int i = 0; i < items.Count; i++)
            {
                accumulatedProbability += items[i].chance;
                if (randomNumber <= accumulatedProbability)
                    return items[i];
            }
            return null;    // this code will never come while you use this programm right :)
        }
    }
    
    // Example of using somewhere in your program:
            static void Main(string[] args)
            {
                List<Item> items = new List<Item>();
                items.Add(new Item() { name = "Anna", chance = 100});
                items.Add(new Item() { name = "Alex", chance = 125});
                items.Add(new Item() { name = "Dog", chance = 50});
                items.Add(new Item() { name = "Cat", chance = 35});
    
                Item newItem = ProportionalWheelSelection.SelectItem(items);
            }
    
    0 讨论(0)
  • 2020-12-05 11:14

    The general approach is to feed uniformly distributed random numbers from 0..1 interval into the inverse of the cumulative distribution function of your desired distribution.

    Thus in your case, just draw a random number x from 0..1 (for example with Random.NextDouble()) and based on its value return

    • 1 if 0 <= x < 150/208,
    • 2 if 150/208 <= x < 190/208,
    • 3 if 190/208 <= x < 205/208 and
    • 4 otherwise.
    0 讨论(0)
提交回复
热议问题