Adam Reed - 3 days ago 5x

C# Question

I am attempting to build a function that will generate random numbers that sum to the provided value. However I also need those values to be greater than the provided value.

My current attempt:

The process: (Example of N = 10, M = 3)

Generate a list of random numbers (N-1) in length.

Add 0 and M (3) to that list.

Sort the List.

Take the differences of the Adjacent Numbers.

**This provides a nice random breakdown of the Sum with Uniform amounts. However, now I need to limit those amounts to a Minimum Value. I am stumped.**

**Example:**

`var r = new Random();`

var N = 10;

var M = 3*100;

var startingList = new List<int>();

for (int i = 1; i <= N - 1; i++)

{

startingList.Add(r.Next(0, M));

}

startingList.Add(0);

startingList.Add(M);

startingList = startingList.OrderBy(o => o).ToList();

startingList.ForEach(Console.WriteLine);

var a = 0;

var b = 1;

var randomList = new List<double>();

for (int i = 1; i <= startingList.Count-1; i++)

{

double difference = (Convert.ToDouble(startingList[b]) - Convert.ToDouble(startingList[a]))/100;

randomList.Add(difference);

a++;

b++;

}

randomList.ForEach(f => Console.Write(f + ", "));

Console.WriteLine("Sum = " + randomList.Sum());

Console.ReadLine();

`0`

33

84

142

175

209

230

245

272

298

300

0.33, 0.51, 0.58, 0.33, 0.34, 0.21, 0.15, 0.27, 0.26, 0.02, Sum = 3

0

1

2

5

75

101

140

203

204

295

300

0.01, 0.01, 0.03, 0.7, 0.26, 0.39, 0.63, 0.01, 0.91, 0.05, Sum = 3

0

26

44

73

83

96

140

168

178

189

300

0.26, 0.18, 0.29, 0.1, 0.13, 0.44, 0.28, 0.1, 0.11, 1.11, Sum = 3

Answer

If your minimum value is `min`

and you generate `N`

numbers, then you will end up with a sum of at least `min * N`

. So the remaining space `M - min * N`

is the space where you can sample randomly. Here is the general idea:

```
samples := N random samples in [0, 1)
sum := sum(samples)
for each s in samples
emit min + s * (M - min * N) / sum
```

Source (Stackoverflow)

Comments