简体   繁体   English

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

[英]Generate Random number in a range with no repeatation

I am trying to randomize quiz questions from firebase. 我正在尝试随机分配来自Firebase的测验问题。 I am trying to generate random numbers from 0-50 add them in a array and once the number is used I want that number to be removed. 我试图从0-50生成随机数,将它们添加到数组中,一旦使用了数字,我希望删除该数字。 But it goes forever with repeatation, here is piece of code that I use to generate random 但这永远伴随着重复,这是我用来生成随机数的一段代码

I am calling updatequestion() on oncreate and 4 choice butons of the quiz. 我在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);

    }
}

By shuffling the list you have all numbers from 0 to 49 randomly positioned in the list: 通过对列表进行混洗,您可以将0到49之间的所有数字随机放置在列表中:

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

You have this code in your updateQuestion() . 您的updateQuestion()有此代码。

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

So if questionNos already has 49 questions from a previous call to updateQuestion() you will get 99 questions, with 98 of them duplicate, etc. 因此,如果questionNos updateQuestion()在先前对updateQuestion()调用中已经有49个问题,您将获得99个问题,其中98个重复,等等。

Move this to somewhere where it is called only once, like the constructor. 将其移动到仅被调用一次的位置,例如构造函数。

Use this piece of Code 使用这段代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM