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 58k
  • Answers 58k
  • 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 Ok, implemented it myself in the end. I did a… May 11, 2026 at 8:39 am
  • added an answer It's an oddity of the DOM validation that happens in… May 11, 2026 at 8:39 am
  • added an answer You can output XML directly from SQL Server 2005 using… May 11, 2026 at 8:39 am

Top Members

Trending Tags

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

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.

      Related Questions

      No related questions found