2

Suppose I have a range of values from 1 to 10. I want to randomly pick a number from this range based on some kind of gaussian probability. So, there is a higher chance to get the values at about 4,5,6. There are still chances of getting 1 and 10, but the chance is lesser. It doesn't need to exactly follow a gaussian distribution because then the values of 1 and 10 is almost near impossible to get. What I wish is to at least alter and skew the chance of getting the extreme values while spreading the chances of getting other values as well. By skewing, I mean there could be times I would want the chance of getting 10 to be more than 1. So the distribution may not necessarily be symmetrical.

I tried playing around with seeding a random float value using the Random class and multiplying the value by 10, but the probably is still linear. I can't think of how I could do that. Is there a simple way to achieve this?

Carven
  • 14,988
  • 29
  • 118
  • 161
  • This may be relevant: http://stackoverflow.com/questions/218060/random-gaussian-variables – Matthew Watson Apr 01 '13 at 18:17
  • The first google result for "C# gaussian random" gave: [Random Gaussian Variables](http://stackoverflow.com/questions/218060/random-gaussian-variables). Did you try that? – publicgk Apr 01 '13 at 18:18

2 Answers2

2

In C#, values from Random have a uniform distribution, so multiplying them by some constant is simply going to shift the range of your values.

However, using the Box-Muller transform, you can create a Gaussian distribution using two uniform random variables.

Check out the answer to this question for an implementation.

Community
  • 1
  • 1
dckrooney
  • 3,041
  • 3
  • 22
  • 28
  • Maybe I should have made my question clearer. I wasn't really looking for an exact gaussian distribution. The gaussian distribution is symmetrical. I was hoping that I could have the distribution to at times skewed towards one side or the other side. – Carven Apr 01 '13 at 18:27
  • @Carven That's certainly possible. You'll need to determine exactly what you want and how to create an algorithm to generate such a distribution based on some number of evenly distributed input values. The exact implementation will depend on the specifics of the distribution you want. – Servy Apr 01 '13 at 18:28
1

This may helps you:

Apply your probability function to values 1 to 10. Then you get their frequencies. Now make an array with values from 1 to 10 each repeated based on it's frequency. e.g:

int[] values = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 10 };

Now you can use a Random variable and get a number (index) between 0 and values.Length. Finally return the values[index].

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
  • 1
    pre-computing a lookup like this is quite effective for distribution functions that aren't all that complex when the range they're applied to is both static and small. If any of those assertions doesn't apply (and I doubt they all do) then this isn't a practical solution. – Servy Apr 01 '13 at 18:29