Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6541423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:06:13+00:00 2026-05-25T11:06:13+00:00

do you know a way to split an integer into say… 5 groups. Each

  • 0

do you know a way to split an integer into say… 5 groups.
Each group total must be at random but the total of them must equal a fixed number.

for example I have “100” I wanna split this number into

1- 20
2- 3
3- 34
4- 15
5- 18

EDIT: i forgot to say that yes a balance would be a good thing.I suppose this could be done by making a if statement blocking any number above 30 instance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:06:14+00:00Added an answer on May 25, 2026 at 11:06 am

    Depending on how random you need it to be and how resource rich is the environment you plan to run the script, you might try the following approach.

    <?php
    set_time_limit(10);
    
    $number_of_groups   = 5;
    $sum_to             = 100;
    
    $groups             = array();
    $group              = 0;
    
    while(array_sum($groups) != $sum_to)
    {
        $groups[$group] = mt_rand(0, $sum_to/mt_rand(1,5));
    
        if(++$group == $number_of_groups)
        {
            $group  = 0;
        }
    }
    

    The example of generated result, will look something like this. Pretty random.

    [root@server ~]# php /var/www/dev/test.php
    array(5) {
      [0]=>
      int(11)
      [1]=>
      int(2)
      [2]=>
      int(13)
      [3]=>
      int(9)
      [4]=>
      int(65)
    }
    [root@server ~]# php /var/www/dev/test.php
    array(5) {
      [0]=>
      int(9)
      [1]=>
      int(29)
      [2]=>
      int(21)
      [3]=>
      int(27)
      [4]=>
      int(14)
    }
    [root@server ~]# php /var/www/dev/test.php
    array(5) {
      [0]=>
      int(18)
      [1]=>
      int(26)
      [2]=>
      int(2)
      [3]=>
      int(5)
      [4]=>
      int(49)
    }
    [root@server ~]# php /var/www/dev/test.php
    array(5) {
      [0]=>
      int(20)
      [1]=>
      int(25)
      [2]=>
      int(27)
      [3]=>
      int(26)
      [4]=>
      int(2)
    }
    [root@server ~]# php /var/www/dev/test.php
    array(5) {
      [0]=>
      int(9)
      [1]=>
      int(18)
      [2]=>
      int(56)
      [3]=>
      int(12)
      [4]=>
      int(5)
    }
    [root@server ~]# php /var/www/dev/test.php
    array(5) {
      [0]=>
      int(0)
      [1]=>
      int(50)
      [2]=>
      int(25)
      [3]=>
      int(17)
      [4]=>
      int(8)
    }
    [root@server ~]# php /var/www/dev/test.php
    array(5) {
      [0]=>
      int(17)
      [1]=>
      int(43)
      [2]=>
      int(20)
      [3]=>
      int(3)
      [4]=>
      int(17)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

does anyone know of a way to split all classes in one solution into
How do you split multi-line string into lines? I know this way var result
I am looking for a way to split PascalCase strings, e.g. MyString, into separate
I need to create an easy way to split up some strings into formatted
In PHP, what is the best way to split a string into an array
I want to know is there any way to split text like this: 123456789
My code is split into two main implementations: MenuController.m and AppController.m, each with header
I would like to know how to implement a way to get a random
I'd like to know if there is a shorter / simpler way to: Split
I need to split a string into newlines in .NET and the only way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.