【问题标题】:Generate Random no between x and y whose sum is m在 x 和 y 之间生成总和为 m 的随机数
【发布时间】:2018-04-21 18:03:51
【问题描述】:

我想生成 n 个随机数,其总和应为 m,并且这些数应始终在 x 和 y 之间。

例如:- 如果我说我需要 20 个不,介于 3 和 9 之间,其总和应该是 100。

我想指定 no 的总和和所需数量,以及应该生成随机 no 的限制,它应该给我指定数量的随机 no。下面是我的代码,它生成的 no 大于 2,但我无法设置最大限制。此外,出来的总和和没有的总和。它生成的不等于指定的总和。我完全糊涂了请帮帮我。

TextView textView,randomTotaltxt;
int count0 = 0;
int count1 = 0;
int targetSum = 100;
int numberofDraws = 20;

    textView = (TextView) findViewById(R.id.textview);
    randomTotaltxt = (TextView) findViewById(R.id.randomTptalText);

    Random r = new Random();
    ArrayList<Integer> load = new ArrayList<>();

    //random no
    int sum = 0;
    for (int i=0; i<numberofDraws;i++){
        int next = r.nextInt(targetSum)+1;
        Log.d("No",""+next);
        load.add(next);
        sum += next;
    }

    //scale it
    double scale = 1d*targetSum/sum;
    sum=0;
    for (int i=0; i<numberofDraws;i++){
        load.set(i, (int)(load.get(i)*scale));
        sum += load.get(i);
    }

   while (load.contains(0)|| load.contains(1)){
       for (int i=0; i<load.size();i++) {
           if (load.get(i).equals(0)) {
               load.set(i, load.get(i) + 2);
               count0++;
               sum = sum+1;
           }
           if (load.get(i).equals(1)) {
               load.set(i, load.get(i) + 2);
               sum += load.get(i);
               count1++;
              sum = sum+1;
           }
       }
   }

    // take rounding
    while (sum++ <targetSum){
        int i = r.nextInt(numberofDraws);
        load.set(i, load.get(i) + 1);
    }

    textView.setText(""+load);
    randomTotaltxt.setText(""+(sum-1));

【问题讨论】:

    标签: android random logic


    【解决方案1】:

    这可能不是最佳解决方案,但可以根据需要完成您的工作。

    int targetSum = 100;
    int numberOfDraws = 20;
    int uppr=0;
    int countt =0;
    ArrayList<Integer> load = new ArrayList<>();
    Random random;
    TextView textView1,randomTotaltxt1;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        textView1 = (TextView) findViewById(R.id.textview1);
        randomTotaltxt1 = (TextView) findViewById(R.id.randomTptalText1);
    
        random = new Random();
    
        //random no
        int sum = 0;
        for (int i=0; i<numberOfDraws;i++){
            int next = random.nextInt(targetSum)+1;
            load.add(next);
            sum += next;
        }
    
        //scale it
        double scale = 1d*targetSum/sum;
        sum=0;
        for (int i=0; i<numberOfDraws;i++){
            load.set(i, (int)(load.get(i)*scale));
            sum += load.get(i);
        }
    
        // take rounding
        while (sum++ <targetSum){
            int i = random.nextInt(numberOfDraws);
            load.set(i, load.get(i) + 1);
            Log.d("TAG",""+load);
        }
    
        checkForUpperRange();
    }
    
    public void checkForUpperRange(){
        for (int i=0; i<numberOfDraws;i++){
            int n = load.get(i);
    
            if (n > 9){
                load.set(i, 9);
                uppr = n - 9;   // getting the required no.
                adjustUpper();
                break;
            }
    
            checkForLowerRange();
        }
    }
    
    private void adjustUpper() {
        for (int j=0; j<numberOfDraws;j++){
            if (load.get(j) > 2){
                load.set(j, load.get(j)+uppr);    // uppr is added
                checkForUpperRange();
                break;
            }
        }
    }
    
    public void checkForLowerRange(){
        for (int i=0; i<numberOfDraws;i++){
            int n = load.get(i);
    
            if (n == 0){
                load.set(i, load.get(i)+3); // 3 is added in sum
                adjust0();
                break;
            }
    
            if (n == 1){
                load.set(i, load.get(i)+2); // 2 is added in sum
                adjust1();
                break;
            }
    
            if (n == 2){
                load.set(i, load.get(i)+1); // 1 is added in sum
                adjust2();
                break;
            }
    
            getCount();
        }
    }
    
    public void getCount(){
        countt = 0;
        for (int k=0; k<numberOfDraws;k++){
            countt = countt+ load.get(k);
            showResult();
        }
    }
    
    public void showResult(){
        textView1.setText(""+load);
        randomTotaltxt1.setText(""+countt);
    }
    
    public void adjust0(){
        for (int j=0; j<numberOfDraws;j++){
            if (load.get(j) > 3){
                load.set(j, load.get(j)-3);    // 3 is subtracted
                checkForLowerRange();
                break;
            }
        }
    }
    
    public void adjust1(){
        for (int j=0; j<numberOfDraws;j++){
            if (load.get(j) > 3){
                load.set(j, load.get(j)-2);    // 2 is subtracted
                checkForLowerRange();
                break;
            }
        }
    }
    
    public void adjust2(){
        for (int j=0; j<numberOfDraws;j++){
            if (load.get(j) > 3){
                load.set(j, load.get(j)-1);    // 1 is subtracted
                checkForLowerRange();
                break;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      • 2014-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多