Monday, May 20, 2024
HomeGame Developmentmathematics - C#, Split a number x into y parts with a...

mathematics – C#, Split a number x into y parts with a min and max part_size


For a game I need to split a number into random parts, where the parts have lower and upper limits. (a precheck will ensure that a split is possible)

Example: Number:10, Parts:5, MinSize:1, MaxSize:3
Split(10,5,1,3) => 3,3,2,1,1
Split(10,5,1,3) => 1,2.5,3,1.5,2

I started with:

Foreach Part => Rand(MinSize,MaxSize)
Then I scale all the parts, so that the sum is equal to the number.
BUT, the simple scaling will possibly set parts outside the limits.

Is there a simple implementation with C#?

(For simplicity, the number could be 1 and the limits respectively smaller)
The result of 10,5,1,3 could be scaled from 1,5,0.1,0.3

Greetings,
Mech

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments