lundi 25 juin 2018

Java - how to divide an integer into 5 random parts

I'm working on an android graphics app, and at some point in the code, I need to divide lets say, a rectangle's width into 5 random sizes.

I have my randomintegerfunction(int min, int max) in my activity, but that can help me divide it into 2 parts.

How do I go about dividing an integer, lets say 100, into 5 random parts, so that the first one or two parts arent always the biggest, then I subdivide for the third, fourth and fifth parts?

Right now, I am know I can try to implememt it using my random integer generator,but the issue, I think is that I'd have to use some forced divisions, like dividing the first 70% of the integer into 2 parts, then dividing the remaining 20% into two parts, to make a total of 5 parts, but such a method would always make the first part be bigger than the fifth part, which I'd like to avoid...to make it truly random.

What I'd like, for example...

the first part to potentially be 7, second part 25, third part 5, fourth part 40, fifth/last/remaining part 23. To add up to 100 (or any integer).

I am not sure about how to write the logic of such a function...so please if you have any ideas of how to implement a function that randomly divides an integer into 3 or 4 or 5 or 6 truly random sizes/parts, please enlighten me!

Thanks for your time!




Aucun commentaire:

Enregistrer un commentaire