Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dividing a number into random unequal parts [closed]

So I am trying to make a script that will randomly split a value of 4 into 12 different variables. I can't think of a good way to do this properly. I thought about randomizing numbers so that they are close to 0.33 (1/12 of 4) but that would often lead to the last few numbers being underprioritized. Anyone tried anything like this before or have any great ideas for how to make this as random and evenly uneven as possible?

like image 993
Tom Avatar asked Feb 19 '13 13:02

Tom


People also ask

How do you divide numbers equally?

To "divide evenly" means that one number can be divided by another without anything left over. In other words no remainder! But 7 cannot be evenly divided by 2, as there will be one left over.

How do you divide a number into equal parts in Python?

Use the math. modf() method to split a number into integer and decimal parts, e.g. result = math.


3 Answers

Generate 12 random numbers from your favourite random number generator, call them r1..r12.

Add them all up, call the sum sum.

Your first random fraction of 4 is (r1/sum)*4. The rest should be obvious.

like image 193
High Performance Mark Avatar answered Oct 21 '22 01:10

High Performance Mark


Generate any random number greater than 1 and after rescale to required sum.

Example:

  • Required sum: 4
  • Random Numbers: 1 2 3 4 5 6 7 8 9 10 11 12
  • Sum = 78
  • Rescaled numbers:

1 rescaled to (1*4) / 78

2 rescaled to (2*4) / 78

...

12 rescaled to (12*4) / 78

like image 22
Толя Avatar answered Oct 21 '22 02:10

Толя


The following algorithm provides uniformly distributed partitions, assuming that it is possible to generate uniformly distributed random numbers over a continuous range (or, at least, over a discrete range with sufficiently many possible values that the odds of duplication are negligible).

To produce a partition of t into k values:

  • Generate k-1 uniformly distributed values in the range [0, t].

  • Sort them, and add 0 at the beginning and t at the end.

  • Use the adjacent differences as the partition.

like image 31
rici Avatar answered Oct 21 '22 01:10

rici