在x和y之间生成随机数,其和为m

时间:2017-11-08 21:35:16

标签: android random logic

我想生成n个随机数,其总和应该是m,这些不应该总是在x和y之间。

例如: - 如果我说我需要20不,3到9之间,其总和应为100.

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

lines

1 个答案:

答案 0 :(得分:0)

这可能不是最佳解决方案,但这将按要求完成您的工作。

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;
        }
    }
}