生成一个无重复范围内的随机数

时间:2018-11-01 18:57:55

标签: java android random

我正在尝试随机从firebase进行测验。我试图从0-50生成随机数,将它们添加到数组中,一旦使用了数字,我希望删除该数字。但这永远伴随着重复,这是我用来生成随机数的代码

我在测验的oncreate和4个选择按钮上调用updatequestion()。

public void updateQuestion (){

    for (int i = 0; i < 50; i++) {
        questionNos.add(i);
    }
    Random r = new Random();
    index = r.nextInt(questionNos.size()-1);
    mQuestionNo = questionNos.remove(index);

    mQuestionRef = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/question");
    mQuestionRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String question = dataSnapshot.getValue(String.class);
            questiontextview.setText(question);

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mChoice1Ref = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/choice1");
    mChoice1Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice1 = dataSnapshot.getValue(String.class);
            bchoice1.setText(choice1);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mChoice2Ref = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/choice2");
    mChoice2Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice2 = dataSnapshot.getValue(String.class);
            bchoice2.setText(choice2);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mChoice3Ref = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/choice3");
    mChoice3Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice3 = dataSnapshot.getValue(String.class);
            bchoice3.setText(choice3);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mChoice4Ref = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/choice4");
    mChoice4Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice4 = dataSnapshot.getValue(String.class);
            bchoice4.setText(choice4);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mAnswerRef  = new Firebase("https://class9notes-2808b.firebaseio.com/"+mQuestionNo+"/answer");
    mAnswerRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String answer = dataSnapshot.getValue(String.class);
            mAnswer = dataSnapshot.getValue(String.class);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

    if (questionNos == null ){
        Intent i = new Intent(qiozphy.this,Score.class);
        i.putExtra("Score",mScore);
        startActivity(i);

    }
}

3 个答案:

答案 0 :(得分:1)

通过对列表进行混排,您可以将0到49之间的所有数字随机放置在列表中:

questionNos.clear();  // if not empty 
for (int i = 0; i < 50; i++) {
    questionNos.add(i);
}
Collections.shuffle(questionNos);

答案 1 :(得分:1)

使用这段代码

ArrayList<Integer> numbers = new ArrayList<Integer>();   
Random randomGenerator = new Random();
while (numbers.size() < 50) {

    int random = randomGenerator.nextInt(50); // will generate a random number from 0 to 50
    if (!numbers.contains(random)) {  //will check whether the number is repeated or not
        numbers.add(random); //if number is not repeated then it will add it in array
    }
}

答案 2 :(得分:0)

您的updateQuestion()中有此代码。

 for (int i = 0; i < 50; i++) {
        questionNos.add(i);
 }

因此,如果questionNos在上次致电updateQuestion()时已经有49个问题,您将获得99个问题,其中98个重复,等等。

将其移动到仅被调用一次的位置,例如构造函数。