Solved

C# dividing a number into 24 random pieces/shares

Posted on 2013-05-12
4
619 Views
Last Modified: 2013-05-12
Hey guys,

I'm trying to figure out the easiest way to divide a number into 24 parts, but completely random.

For example, if the number is 15,500 how would I be able to divide it up randomly into 24 parts like this:

1. 253
2. 442
3. 101
4. 788
5. 2,232
etc....

The sum of these 24 random pieces should = 15,500.

Any thoughts?
0
Comment
Question by:mcainc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39159309
should they all be different?
0
 

Author Comment

by:mcainc
ID: 39159314
yeah, all 24 pieces should be different random numbers that all add up to the desired larger number
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39159322
here it is:
        static Random rand = new Random();
        public static IEnumerable<int> Split(Int32 val, int parts)
        {
            int left = val;
            for (int i = 0; i < parts - 1; i++)
            {
                var curr = rand.Next(1, left / parts);
                yield return curr;
                left -= curr;
            }
            yield return left;
        }


        static void Main(string[] args)
        {
            Int32 val = 15500;
            var res = Split(val, 24);
            int sum = 0;
            foreach (var item in res)
            {
                Console.WriteLine(item);
                sum += item;
            }
            var match = (sum == val);

        }

Open in new window

0
 

Author Closing Comment

by:mcainc
ID: 39159415
nice!!! thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question