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 108103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:51:33+00:00 2026-05-11T01:51:33+00:00

How do I generate 30 random numbers between 1-9, that all add up to

  • 0

How do I generate 30 random numbers between 1-9, that all add up to 200 (or some arbitrary N), in C#?

I’m trying to generate a string of digits that can add together to be N.

  • 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. 2026-05-11T01:51:33+00:00Added an answer on May 11, 2026 at 1:51 am

    I’m not sure what the statistics are on this but, the issue here is that you don’t want to randomly select a number that makes it impossible to sum N with M number of entries either by overshooting or undershooting. Here’s how I would do it:

    static void Main() {     int count = 30;     int[] numbers = getNumbers(count, 155);     for (int index = 0; index < count; index++)     {         Console.Write(numbers[index]);         if ((index + 1) % 10 == 0)             Console.WriteLine('');         else if (index != count - 1)             Console.Write(',');     }     Console.ReadKey(); } static int[] getNumbers(int count, int total) {     const int LOWERBOUND = 1;     const int UPPERBOUND = 9;      int[] result = new int[count];     int currentsum = 0;     int low, high, calc;      if((UPPERBOUND * count) < total ||         (LOWERBOUND * count) > total ||         UPPERBOUND < LOWERBOUND)         throw new Exception('Not possible.');      Random rnd = new Random();      for (int index = 0; index < count; index++)     {         calc = (total - currentsum) - (UPPERBOUND * (count - 1 - index));         low = calc < LOWERBOUND ? LOWERBOUND : calc;         calc = (total - currentsum) - (LOWERBOUND * (count - 1 - index));         high = calc > UPPERBOUND ? UPPERBOUND : calc;          result[index] = rnd.Next(low, high + 1);          currentsum += result[index];     }      // The tail numbers will tend to drift higher or lower so we should shuffle to compensate somewhat.      int shuffleCount = rnd.Next(count * 5, count * 10);     while (shuffleCount-- > 0)         swap(ref result[rnd.Next(0, count)], ref result[rnd.Next(0, count)]);      return result; } public static void swap(ref int item1, ref int item2) {     int temp = item1;     item1 = item2;     item2 = temp; } 

    I didn’t have a lot of time to test this so apologies if there’s a flaw in my logic somewhere.

    EDIT:

    I did some testing and everything seems solid. If you want a nice pretty spread it looks like you want something along the lines of Total = Count * ((UPPER + LOWER) / 2). Although I’m fairly certain that as the difference between UPPER and LOWER increases the more flexible this becomes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Maybe you are comparing the 'distance as the crow flies'… May 11, 2026 at 9:57 am
  • added an answer but I don't need (nor want) a main() in the… May 11, 2026 at 9:57 am
  • added an answer Original answer (left so people's votes aren't misrepresented): Current IDE… May 11, 2026 at 9:57 am

Related Questions

How do I generate 30 random numbers between 1-9, that all add up to
How do I generate an ETag HTTP header for a resource file?
How do I generate all the permutations of a list? For example: permutations([]) []
How do I generate a range of consecutive numbers (one per line) from a
How do I generate a random number between 0 and n ?
How do I generate friendly URLs within the ASP.NET MVC Framework? For example, we've
How do I generate excel reports with rich formatting including charts with a ASP.Net
How do I generate an audio sine or square wave of a given frequency?
How do I setup an Ant task to generate Emma code coverage reports?
Do you generate your data dictionary? If so, how? I use extended procedures in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.