I'm trying to do as the title suggests. Take in a value like 100 with a part number like 5. Then split 100 into 5 parts that add up to 100. Each part being random. So a result would be like 20, 25, 5, 40, 10. It would return a list/array. This is the code I'm currently using thanks to a post here from 10+ years ago.
List<int> a = new List<int>();
a = Enumerable.Repeat(0, numOfStats - 1) // Seq with (n-1) elements...
.Select(x => Random.Range(1, points)) // ...mapped to random values
.Concat(new[] { 0, points })
.OrderBy(x => x)
.ToArray()
.ToList();
return a.Skip(1).Select((x, i) => x - a[i]).ToList();
numStats is the division number and points is the total value that will be split.
The only problem is that I need to make sure each part is no more than a certain number. So each part would be max 30 for example. Anyone know how I can edit this to make sure there is a clamp on the parts?
Give up on trying to do it in one line (and program defensively, there are quite a few edge cases)
EDIT Added
SplitValue2()(an improvement overSplitValue()) andShuffle()I don't claim that this is bug-free, you will need to test (and curious to see what other ideas are offered)
Sample output