Random pieces of number

Hello folks,

i have number for example 10 and i want to divide into 4 random pieces that may be (6+2+1+1). How can i do this via script i have random number 234951 and i want to divide into 31 pieces.

One somewhat hacky way:

x=30;n=234951; while [ $x -gt 0 ]; do y=$(expr \( $RANDOM \* $RANDOM \) % \( $n - $x - 1 \) + 1 ); n=$(($n-$y)); echo $y; x=$(($x-1)); done;echo $n

not clean at all, but you may take some inspiration from it :slight_smile:

Thanks so much, but is printing 1 and 2 is it possible it should be in thousands and the sum is not equal to 234951


229761
3831
1186
52
47
1
36
13
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2

not the best random, and still hacky, but how about:

#  x=30;n=234951; while [ $x -gt 0 ]; do y=$(expr \( $RANDOM \* $RANDOM \) % \( $n - 2001 \* $x \) + 1001 ); n=$(($n-$y)); echo $y; x=$(($x-1)); done;echo $n
115320
51912
4445
8766
2573
2351
2379
1523
1850
1436
3091
1531
2184
1942
2571
2049
1817
1685
1915
1758
1356
2592
1634
1355
3023
1499
3191
1021
2965
1464
1753

This is very near but it always show first value bigger one, is it possible will show equal near about equal numbers, lets suppose if my value is 501752 so it first shows 408086, and rest of 29 values are too small, is it possible all 30 values should be 5 digits base.

ok...

If you want to keep all numbers within about +/- 50% of the average, then you could use something like:

x=30;n=234951;o=$(($n/$x));n=$(($n-$o));while [ $x -gt 0 ]; do   a=$(($n/$x));  k=$(($a/2));  y=$(($RANDOM%$a+$k));  n=$(($n-$y));  echo $y;  x=$(($x-1));  done; echo $(($n+$o))

Think that should work...:slight_smile:

1 Like

Thanks so much, you made it very easy, can you please explain the detail of this small trick.