### Thread: Question / Design approach for meeting distribution curve

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2013
Posts
2
Rep Power
0

#### Question / Design approach for meeting distribution curve

Hi folks,

Newbie poster here. I'm working on a project in C#, and I'm curious about your ideas for how to approach something.

This application is going to generate a random list of X number of Things. Each Thing is of a certain Type. The Types need to follow a particular distribution curve. For example:

Type A 5%
Type B 10%
Type C 50%
Type D 35%

So, the idea is, if I choose to generate 100 things, I would get 5 of Type A, 10 of Type B, 50 of Type C, and 35 of Type D. This is a very simple example, of course. In reality there are many more types with various frequencies.

What approach would you take to something like this? The main approach I was taking is as follows:

1. Treat the frequencies as integer values
2. Sum the total of the frequencies
3. Generate a random number between 1 and the total.
4. Loop through the Things, increasing a counter by the frequency of that thing.
5. If the random number is in the current range, then select that thing.

Thoughts on this approach?

In this method, will the results sets over time approximate the ideal distribution? Probability was never my strong point. If I wanted a little MORE variability in the distribution, I wonder how I could introduce that?

Thoughts greatly appreciated.

Steve
2. If you want randomness, rely on the Random class:

Code:
```Random random = new Random();
int randomNumber;

for(int i=0; i < = numObjectsYouNeed; i++)
{
randomNumber =  random.Next(0, 99); // only returns ints
if(randomNumber >= 0 && randomNumber <= 4)
spawnTypeA();
if(randomNumber >= 5 && randomNumber <= 14)
spawnTypeB();
if(randomNumber >= 15 && randomNumber <= 64)
spawnTypeC();
if(randomNumber >= 65 && randomNumber <= 99)
spawnTypeB();
}```
If you see that this is not random enough for you, try changing the time seed.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2013
Posts
2
Rep Power
0
Awesome, thanks very much!