Given n numbers, each with some frequency of occurrence. Return a random number with probability proportional to its frequency of occurrence.

Example:

Let following be the given numbers.
  arr[] = {10, 30, 20, 40}  

Let following be the frequencies of given numbers.
  freq[] = {1, 6, 2, 1}  

The output should be
  10 with probability 1/10
  30 with probability 6/10
  20 with probability 2/10
  40 with probability 1/10

It is quite clear that the simple random number generator won’t work here as it doesn’t keep track of the frequency of occurrence.

We need to somehow transform the problem into a problem whose solution is known to us.

One simple method is to take an auxiliary array (say aux[]) and duplicate the numbers according to their frequency of occurrence. Generate a random number(say r) between 0 to Sum-1(including both), where Sum represents summation of frequency array (freq[] in above example). Return the random number aux[r] (Implementation of this method is left as an exercise to the readers).

[ad type=”banner”]

The limitation of the above method discussed above is huge memory consumption when frequency of occurrence is high. If the input is 997, 8761 and 1, this method is clearly not efficient.

How can we reduce the memory consumption? Following is detailed algorithm that uses O(n) extra space where n is number of elements in input arrays.

1. Take an auxiliary array (say prefix[]) of size n.
2. Populate it with prefix sum, such that prefix[i] represents sum of numbers from 0 to i.
3. Generate a random number(say r) between 1 to Sum(including both), where Sum represents summation of input frequency array.
4. Find index of Ceil of random number generated in step #3 in the prefix array. Let the index be indexc.
5. Return the random number arr[indexc], where arr[] contains the input n numbers.

Before we go to the implementation part, let us have quick look at the algorithm with an example:
arr[]: {10, 20, 30}
freq[]: {2, 3, 1}
Prefix[]: {2, 5, 6}
Since last entry in prefix is 6, all possible values of r are [1, 2, 3, 4, 5, 6] 1: Ceil is 2. Random number generated is 10.
2: Ceil is 2. Random number generated is 10.
3: Ceil is 5. Random number generated is 20.
4: Ceil is 5. Random number generated is 20.
5: Ceil is 5. Random number generated is 20.
6. Ceil is 6. Random number generated is 30.
In the above example
10 is generated with probability 2/6.
20 is generated with probability 3/6.
30 is generated with probability 1/6.

[ad type=”banner”]

How does this work?
Any number input[i] is generated as many times as its frequency of occurrence because there exists count of integers in range(prefix[i – 1], prefix[i]] is input[i]. Like in the above example 3 is generated thrice, as there exists 3 integers 3, 4 and 5 whose ceil is 5.

[pastacode lang=”c” manual=”%2F%2FC%20program%20to%20generate%20random%20numbers%20according%20to%20given%20frequency%20distribution%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0A%2F%2F%20Utility%20function%20to%20find%20ceiling%20of%20r%20in%20arr%5Bl..h%5D%0Aint%20findCeil(int%20arr%5B%5D%2C%20int%20r%2C%20int%20l%2C%20int%20h)%0A%7B%0A%20%20%20%20int%20mid%3B%0A%20%20%20%20while%20(l%20%3C%20h)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20mid%20%3D%20l%20%2B%20((h%20-%20l)%20%3E%3E%201)%3B%20%20%2F%2F%20Same%20as%20mid%20%3D%20(l%2Bh)%2F2%0A%20%20%20%20%20%20%20%20(r%20%3E%20arr%5Bmid%5D)%20%3F%20(l%20%3D%20mid%20%2B%201)%20%3A%20(h%20%3D%20mid)%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20(arr%5Bl%5D%20%3E%3D%20r)%20%3F%20l%20%3A%20-1%3B%0A%7D%0A%20%0A%2F%2F%20The%20main%20function%20that%20returns%20a%20random%20number%20from%20arr%5B%5D%20according%20to%0A%2F%2F%20distribution%20array%20defined%20by%20freq%5B%5D.%20n%20is%20size%20of%20arrays.%0Aint%20myRand(int%20arr%5B%5D%2C%20int%20freq%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20Create%20and%20fill%20prefix%20array%0A%20%20%20%20int%20prefix%5Bn%5D%2C%20i%3B%0A%20%20%20%20prefix%5B0%5D%20%3D%20freq%5B0%5D%3B%0A%20%20%20%20for%20(i%20%3D%201%3B%20i%20%3C%20n%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20prefix%5Bi%5D%20%3D%20prefix%5Bi%20-%201%5D%20%2B%20freq%5Bi%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20prefix%5Bn-1%5D%20is%20sum%20of%20all%20frequencies.%20Generate%20a%20random%20number%0A%20%20%20%20%2F%2F%20with%20value%20from%201%20to%20this%20sum%0A%20%20%20%20int%20r%20%3D%20(rand()%20%25%20prefix%5Bn%20-%201%5D)%20%2B%201%3B%0A%20%0A%20%20%20%20%2F%2F%20Find%20index%20of%20ceiling%20of%20r%20in%20prefix%20arrat%0A%20%20%20%20int%20indexc%20%3D%20findCeil(prefix%2C%20r%2C%200%2C%20n%20-%201)%3B%0A%20%20%20%20return%20arr%5Bindexc%5D%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%20%3D%20%7B1%2C%202%2C%203%2C%204%7D%3B%0A%20%20%20%20int%20freq%5B%5D%20%3D%20%7B10%2C%205%2C%2020%2C%20100%7D%3B%0A%20%20%20%20int%20i%2C%20n%20%3D%20sizeof(arr)%20%2F%20sizeof(arr%5B0%5D)%3B%0A%20%0A%20%20%20%20%2F%2F%20Use%20a%20different%20seed%20value%20for%20every%20run.%0A%20%20%20%20srand(time(NULL))%3B%0A%20%0A%20%20%20%20%2F%2F%20Let%20us%20generate%2010%20random%20numbers%20accroding%20to%0A%20%20%20%20%2F%2F%20given%20distribution%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%205%3B%20i%2B%2B)%0A%20%20%20%20%20%20printf(%22%25d%5Cn%22%2C%20myRand(arr%2C%20freq%2C%20n))%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”C Program” highlight=”” provider=”manual”/]

Output: May be different for different runs

4
3
4
4
4
[ad type=”banner”]