You might be able to use a, if you're happy with the shape of that distribution. Set n=12 and p=0.25. This will give you a value between 0 and 12 with a mean of 3. Just add 2 to each result to get the range and mean you are looking for. Edit: As for implementation, you can probably find a library for your chosen language that supports non-uniform distributions (I've ).
![Random numbers Random numbers](/uploads/1/2/5/4/125407905/469046915.png)
Bit-parallel random number generation for discrete uniform distributions. Author links open overlay panel Pei-Chi Wu a Kuo-Chan Huang b Shih-Ting Ouyang c. YaoThe complexity of nonuniform random number generation. Traub (Ed.), Algorithms and Complexity, Academic Press (1976), pp.
A binomial distribution can be approximated fairly easily using a uniform RNG. Simply perform n trials and record the number of successes. So if you have n=10 and p=0.5, it's just like flipping a coin 10 times in a row and counting the number of heads. For p=0.25 just generate uniformly-distributed values between 0 and 3 and only count zeros as successes. If you want a more efficient implementation, there is a clever algorithm hidden away in the exercises of volume 2 of Knuth's The Art of Computer Programming.
I have an array with $N$ elements. I want to sample elements pseudo-randomly in the array in a controllable fashion. For example, I would like to sample the elements such that element $x$ is sampled $K$ times more than element $N$, where $N$ = Total number of elements $T$ = Some constant, e.g. 3 $K = 1 + T(1 - x/N)$ In this example, the first element is sampled $ approx 4$ times more than the last one, and the penultimate element is sampled $1 + T(1 - (N-1)/N) approx 1$ time(s) more than the last one. Here's a very simple and not great generalized solution to the problem.
Given a list of N elements called elem. Given a list of their respective frequencies called freq. First normalize the list of frequencies so it sums to 1 by diving by some constant. Partition the range of real numbers from 0 to 1 so that each frequency is assigned a portion of the range from 0 to 1 proportional to its size. Have a uniform random number generator generate a number from 0 to 1. This number can then be interpreted as one of the N elements in elem based on freq's partitioning of the range of numbers between 0 and 1.
For example lets say we had three elements with relative frequencies 2, 1, and 1. Normalizing the list we get the they have the respective probabilities 1/2, 1/4, 1/4. Therefore if the random number generator gives a number less than 1/2 we give the first value, then if it is less than 3/4 we give the second value, otherwise we give the third. This is of course a terribly obvious general answer. If you have a specific discrete distribution, then there is often a better way of doing this.