一个数字,比如 37,现在想把他随机分成N分,比如分成1,24,10,0,12这5份
该怎么分?
我是想确定随机。比如,有可能分出(37,0,0,0,0)这样的。

解决方案 »

  1.   

     private int[] suiji(int shuzi, int fenshu)
            {
                int[] shuzu = new int[fenshu];
                int flag = 0;
                Random random;
                for (int i = 0; i < fenshu; i++)
                {
                    random = new Random();
                    if (shuzi - flag == 0)
                        return shuzu;
                    shuzu[i] = random.Next(0, shuzi-flag);
                    flag += shuzu[i];
                }
                return shuzu;
            }shuzi是你要分割的数字 (37)
    fenshu是你要分成几份(5)
    没测试
      

  2.   

     public IList<int> GetInt(int p_Int, int p_Count)
            {
                IList<int> _ReturnList =new List<int>();            int _Max = p_Int ;
                Random _Random = new Random(DateTime.Now.Millisecond);
                for (int i = 0; i != p_Count-1; i++)
                {
                    int _Count = _Random.Next(0, _Max + 1);
                    _Max -= _Count;
                    _ReturnList.Add(_Count);
                }
                _ReturnList.Add(_Max);
                return _ReturnList;
            }
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication235
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            int M = 37,N=5;
                int T=0;
                Random R = new Random();
                String S = String.Empty;            for (int i = 0; i < N-1; i++)
                {
                    S += (T = R.Next(M + 1)).ToString() + " ";
                    M =( M -= T) >= 0 ? M : 0;
                }            S += M.ToString();            MessageBox.Show(S);
            }
        }
    }
    欢迎送分题
      

  4.   


                int n = 5;
                int m = 37;
                //----------------上面的值你自己随便改------------------------
                int remain = m;
                Random r = new Random(Environment.TickCount);
                for (int i = 0; i < n; i++)
                {
                    int j = r.Next(0, remain + 1);
                    Console.WriteLine(j);
                    remain -= j;
                }
      

  5.   

    private static int[] suiji(int shuzi, int fenshu)
            {
                int[] shuzu = new int[fenshu];
                int flag = 0;
                Random random;
                for (int i = 0; i < fenshu; i++)
                {
                    random = new Random();
                    if (shuzi - flag == 0)
                        return shuzu;
                    if (i == fenshu - 1)
                    {
                        shuzu[i] = shuzi - flag;
                        return shuzu;
                    }
                    shuzu[i] = random.Next(0, shuzi-flag);
                    flag += shuzu[i];
                    Thread.Sleep(5);
                }
                return shuzu;
            }
    3楼的有点问题
    最后一份不用随机的,就是剩下的值
      

  6.   

    测试了一下,wartim 的比较真实一些。