取150个随机数,总和等于20000,谢谢.

解决方案 »

  1.   

    不太均匀
    import java.util.*;
    public class T1013 {
    public static void main(String args[]){
    Random rnd = new Random();
    int sum = 20000;
    int tmp = sum;
    for(int i=0; i<149; i++){
    int next = rnd.nextInt(tmp);
    System.out.println(next);
    tmp -= next;
    }
    System.out.println(tmp);
    }
    }
      

  2.   

    不用那么麻烦,一个循环就可以了int max = 20000;
    int[] num = new int[150];
    for (int i=0; i<149; i++) {
        num[i] = (int)(Math.random()*max); //生成随技术
        System.out.print("num["+i+"]="+num[i]+",");
        max -= num[i]; //不断地减去生成的随机数
    }
    num[149] = max;
    System.out.print("num[149]="+max);
      

  3.   

    qybao(阿宝)的方法很好
    不过我觉得产生随机数时乘的那个数要根据要求的范围来确定
    如果用MAX的话后面肯定有一大堆随机数都是零。
      

  4.   

    同意fengxin09(星缘) 
    随即数的生成应该规定服从某个分布
    比如说均值为(20000/150)的正态分布
      

  5.   

    believefym(暮色,miss,迷失,miss)用的是JDK里的java.util.Random类qybao(阿宝)用的是java.lang.Math.random()静态方法,由于是一个静态方法,所以使用时可以不通过Math对象,调用此方法,会先创建出一个Random对象两个人的for循环本质是一样的不过我试了n次,只有前面10来个是正常的数字,以后的都是0了,非常有意思的是最后一个都是1
      

  6.   

    qybao(阿宝)的方法很好
    不过我觉得产生随机数时乘的那个数要根据要求的范围来确定
    如果用MAX的话后面肯定有一大堆随机数都是零。
    ----------------------------------------------
    避免出现一大堆0的情况,稍微改改就可以了
    int max = 20000;
    int[] num = new int[150];
    int count = 150; //平均数
    for (int i=0; i<149; i++) {
        num[i] = (int)(Math.random()*max/count--); //max/count,取平均数的随机数
        System.out.print("num["+i+"]="+num[i]+",");
        max -= num[i]; //不断地减去生成的随机数
    }
    num[149] = max;
    System.out.print("num[149]="+max);
      

  7.   

    qybao(阿宝)  真乃神人也.不过,还是没解决问题. 会有负数出现.
      

  8.   

    to  Riddick2046(神为) ( )
    会有负数出现?请问是哪一步?或者你运行过了?
    我只是按逻辑分析
    首先0<=Math.random()<=1,所以 0<=(Math.random()*max/count--)<=max
    即 num[i]总是小于或等于max
    所以max=max-num[i]总是大于等于0
    两个大于等于0的数相乘又怎么会出现负数呢?
      

  9.   

    int max = 20000/150*2; //根据正态分布和随机数原理,应该设置平均值的2倍作为极限值比较合理
    int[] num = new int[150];
    int sum =0;
    for (int i=0; i<150/2; i++) {
        num[i] = (int)(Math.random()*max);  //生成一个随机数
        num[149-i] = max - num[i]; //每次生成一对随机数,每对随机数的和为max,这样保证了所有随机数总和为20000
        System.out.print("num["+i+"]="+num[i]+",");
        System.out.print("num["+(149-i)+"]="+num[149-i]+",");
        sum+=num[i];
        sum+=num[149-i];
    }
    System.out.print("sum="+sum);
      

  10.   

    楼上的,sum=19950,这个是怎么回事
      

  11.   

    to qybao(阿宝) 
    为啥我运行你那代码后,SUM总不是等于2W呢?
      

  12.   

    to qingyun1136(执迷不悟):int max = 20000/150*2; 这句话就有问题吧,这样得出的MAX会直接导致最后的结果不正确
      

  13.   

    package jimi;public class test {
    public static void main(String[] agrs){
    int max = 20000;
    int num = 150;
    int [] result = new int[num];

    while(max>0){
    int temp = getAmount(1);
    if(max<=temp){
    result[getRandomPos(num)]+=max;

    }else{
    result[getRandomPos(num)]+=temp;
    }
    max-=temp;
    }
    int sum = 0;
    for(int i=0;i<150;i++){
    System.out.println("result["+i+"] = "+result[i]);
    sum += result[i];
    }
    System.out.println("sum = "+sum);
    }
    public static int  getRandomPos(int num){
    return (int)(getRandom()*num);
    }
    public static int  getAmount(int precision){
    return (int)(getRandom()*precision +1);
    }
    public  static double  getRandom(){
    return ((int)(Math.random()*1000))/1000.0;
    }
    }
      

  14.   

    其中getAmount(int precision)中的参数为分配的精度,越小越均匀,越大越差别大,具体的随机数的分布方式我不知道,需要进一步的数学验证
      

  15.   

    感谢 zoeg(沉香) 的那一段话
      

  16.   

    to qybao(阿宝) 
    为啥我运行你那代码后,SUM总不是等于2W呢?
    -----------------------------------------
    不知你是怎么相加不等于2W的?
    你再验证试试
    int max = 20000;
    int[] num = new int[150];
    int count = 150; //平均数
    for (int i=0; i<149; i++) {
        num[i] = (int)(Math.random()*max/count--); //max/count,取平均数的随机数
        System.out.print("num["+i+"]="+num[i]+",");
        max -= num[i]; //不断地减去生成的随机数
    }
    num[149] = max;
    System.out.print("num[149]="+max);
    //验证
    int sum = 0;
    for (int i=0; i<150; i++) {
        sum += num[i];
    }
    System.out.print("sum="+sum);
      

  17.   

    precision参数为1时的一次运行结果
    result[0] = 151
    result[1] = 122
    result[2] = 115
    result[3] = 128
    result[4] = 117
    result[5] = 117
    result[6] = 137
    result[7] = 137
    result[8] = 127
    result[9] = 144
    result[10] = 170
    result[11] = 125
    result[12] = 154
    result[13] = 141
    result[14] = 132
    result[15] = 145
    result[16] = 154
    result[17] = 124
    result[18] = 138
    result[19] = 136
    result[20] = 109
    result[21] = 144
    result[22] = 121
    result[23] = 129
    result[24] = 128
    result[25] = 120
    result[26] = 115
    result[27] = 148
    result[28] = 137
    result[29] = 110
    result[30] = 134
    result[31] = 143
    result[32] = 121
    result[33] = 144
    result[34] = 164
    result[35] = 152
    result[36] = 141
    result[37] = 131
    result[38] = 126
    result[39] = 143
    result[40] = 167
    result[41] = 109
    result[42] = 128
    result[43] = 147
    result[44] = 126
    result[45] = 121
    result[46] = 140
    result[47] = 111
    result[48] = 142
    result[49] = 141
    result[50] = 105
    result[51] = 150
    result[52] = 141
    result[53] = 98
    result[54] = 157
    result[55] = 142
    result[56] = 120
    result[57] = 158
    result[58] = 131
    result[59] = 114
    result[60] = 149
    result[61] = 145
    result[62] = 117
    result[63] = 129
    result[64] = 112
    result[65] = 129
    result[66] = 155
    result[67] = 145
    result[68] = 121
    result[69] = 152
    result[70] = 143
    result[71] = 134
    result[72] = 134
    result[73] = 116
    result[74] = 118
    result[75] = 138
    result[76] = 149
    result[77] = 136
    result[78] = 127
    result[79] = 139
    result[80] = 127
    result[81] = 147
    result[82] = 126
    result[83] = 114
    result[84] = 128
    result[85] = 134
    result[86] = 124
    result[87] = 123
    result[88] = 146
    result[89] = 130
    result[90] = 146
    result[91] = 138
    result[92] = 135
    result[93] = 134
    result[94] = 136
    result[95] = 107
    result[96] = 141
    result[97] = 139
    result[98] = 112
    result[99] = 143
    result[100] = 121
    result[101] = 131
    result[102] = 124
    result[103] = 127
    result[104] = 121
    result[105] = 140
    result[106] = 143
    result[107] = 118
    result[108] = 137
    result[109] = 140
    result[110] = 123
    result[111] = 143
    result[112] = 135
    result[113] = 125
    result[114] = 151
    result[115] = 151
    result[116] = 119
    result[117] = 127
    result[118] = 133
    result[119] = 111
    result[120] = 139
    result[121] = 156
    result[122] = 140
    result[123] = 126
    result[124] = 116
    result[125] = 101
    result[126] = 119
    result[127] = 140
    result[128] = 117
    result[129] = 142
    result[130] = 146
    result[131] = 128
    result[132] = 145
    result[133] = 148
    result[134] = 107
    result[135] = 148
    result[136] = 144
    result[137] = 124
    result[138] = 146
    result[139] = 143
    result[140] = 115
    result[141] = 162
    result[142] = 164
    result[143] = 104
    result[144] = 153
    result[145] = 145
    result[146] = 114
    result[147] = 137
    result[148] = 130
    result[149] = 141
    sum = 20000
      

  18.   

    precision参数为50时的一次运行结果
    result[0] = 108
    result[1] = 121
    result[2] = 111
    result[3] = 245
    result[4] = 140
    result[5] = 70
    result[6] = 169
    result[7] = 207
    result[8] = 83
    result[9] = 158
    result[10] = 72
    result[11] = 131
    result[12] = 106
    result[13] = 90
    result[14] = 167
    result[15] = 134
    result[16] = 154
    result[17] = 132
    result[18] = 220
    result[19] = 155
    result[20] = 23
    result[21] = 131
    result[22] = 168
    result[23] = 88
    result[24] = 89
    result[25] = 70
    result[26] = 74
    result[27] = 274
    result[28] = 278
    result[29] = 182
    result[30] = 265
    result[31] = 279
    result[32] = 35
    result[33] = 138
    result[34] = 112
    result[35] = 170
    result[36] = 213
    result[37] = 57
    result[38] = 129
    result[39] = 146
    result[40] = 121
    result[41] = 86
    result[42] = 154
    result[43] = 86
    result[44] = 69
    result[45] = 161
    result[46] = 213
    result[47] = 80
    result[48] = 106
    result[49] = 169
    result[50] = 84
    result[51] = 36
    result[52] = 103
    result[53] = 126
    result[54] = 20
    result[55] = 114
    result[56] = 165
    result[57] = 327
    result[58] = 119
    result[59] = 161
    result[60] = 95
    result[61] = 170
    result[62] = 95
    result[63] = 313
    result[64] = 68
    result[65] = 172
    result[66] = 213
    result[67] = 93
    result[68] = 130
    result[69] = 180
    result[70] = 68
    result[71] = 55
    result[72] = 197
    result[73] = 135
    result[74] = 156
    result[75] = 174
    result[76] = 29
    result[77] = 84
    result[78] = 93
    result[79] = 100
    result[80] = 77
    result[81] = 110
    result[82] = 146
    result[83] = 113
    result[84] = 202
    result[85] = 237
    result[86] = 95
    result[87] = 101
    result[88] = 121
    result[89] = 166
    result[90] = 121
    result[91] = 125
    result[92] = 68
    result[93] = 122
    result[94] = 72
    result[95] = 143
    result[96] = 22
    result[97] = 139
    result[98] = 74
    result[99] = 173
    result[100] = 166
    result[101] = 51
    result[102] = 231
    result[103] = 144
    result[104] = 96
    result[105] = 184
    result[106] = 135
    result[107] = 129
    result[108] = 137
    result[109] = 73
    result[110] = 183
    result[111] = 168
    result[112] = 202
    result[113] = 129
    result[114] = 155
    result[115] = 167
    result[116] = 43
    result[117] = 9
    result[118] = 91
    result[119] = 129
    result[120] = 77
    result[121] = 275
    result[122] = 125
    result[123] = 162
    result[124] = 85
    result[125] = 113
    result[126] = 91
    result[127] = 174
    result[128] = 150
    result[129] = 83
    result[130] = 103
    result[131] = 92
    result[132] = 172
    result[133] = 188
    result[134] = 64
    result[135] = 218
    result[136] = 279
    result[137] = 40
    result[138] = 61
    result[139] = 112
    result[140] = 168
    result[141] = 186
    result[142] = 179
    result[143] = 47
    result[144] = 75
    result[145] = 242
    result[146] = 210
    result[147] = 96
    result[148] = 208
    result[149] = 62
    sum = 20000
      

  19.   

    支持cly33(jim),不过楼主的150个随机数好像不一定是int型哦