使用线性全等方法公式的shuffilng数组

时间:2017-07-26 16:03:13

标签: java android arrays lcm

我需要一些帮助,我试图使用某个公式(线性全等方法)来改组一系列问题,但我如何将它们应用到我的数组中呢?

> X(i+1) = (a * X(i) + c) mod M

M是模量。 M> 0

a是乘数,0 <= a&lt;微米。

c是增量,0&lt; = c&lt;微米。

X(0)是种子值,0 <= X(0)<0。微米。

我是迭代器。我&lt; M

public class lcm {
    // Linear congruential values for x(i+1) = (a * x(i) + b) % m.
    final static int a = 6;
    final static int b = 9;
    final static int m = 25;

    // Current value for returning.
    int x;

    public lcm() {
        // Constructor simply sets value to half of m, equivalent to 0.5.
        x =0;
    }

    int next() {
        // Calculate next value in sequence.
        x = (a * x + b) % m;

        // Return its 0-to-1 value.
        return (int)x;
    }

    public static void main(String[] args) {
        // Create a new myRnd instance.
        lcm r = new lcm();

        // Output 20 random numbers from it.
        for (int i = 0; i < 25; i++) {
            System.out.println (r.next());
        }
    }
}

使这样的输出

9
13
12
6
20
4
8
7
1
15
24
3
2
21
10
19
23
22
16
5
14
18
17
11
0

尝试通过lcm输出将数组混洗到此列表

public class QuizActivity extends AppCompatActivity {

    private TextView mScoreView, mQuestion;
    private ImageView mImageView;
    private Button mTrueButton, mFalseButton;

    private boolean mAnswer;
    private int mScore = 0;
    private int mQuestionNumber = 0;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);

        mScoreView = (TextView)findViewById(R.id.points);
        mImageView = (ImageView)findViewById(R.id.imageView);
        mQuestion = (TextView)findViewById(R.id.question);
        mTrueButton = (Button)findViewById(R.id.trueButton);
        mFalseButton = (Button)findViewById(R.id.falseButton);

        updateQuestion();

        //Logic for true button
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mAnswer == true) {
                    mScore++;
                    updateScore(mScore);

                    //perform check before you update the question
                    if (mQuestionNumber == questionArray.length) {
                        Intent i = new Intent(QuizActivity.this, ResultsActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt("finalScore", mScore);
                        i.putExtras(bundle);
                        QuizActivity.this.finish();
                        startActivity(i);
                    } else {
                        updateQuestion();
                    }
                }
                else {
                    if (mQuestionNumber == questionArray.length) {
                        Intent i = new Intent(QuizActivity.this, ResultsActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt("finalScore", mScore);
                        i.putExtras(bundle);
                        QuizActivity.this.finish();
                        startActivity(i);
                    } else {
                        updateQuestion();
                    }
                }
            }
        });




        //Logic for false button
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mAnswer == false) {
                    mScore++;
                    updateScore(mScore);

                    //perform check before you update the question
                    if (mQuestionNumber == questionArray.length) {
                        Intent i = new Intent(QuizActivity.this, ResultsActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt("finalScore", mScore);
                        i.putExtras(bundle);
                        QuizActivity.this.finish();
                        startActivity(i);
                    } else {
                        updateQuestion();
                    }
                }
                else {
                    if (mQuestionNumber == questionArray.length) {
                        Intent i = new Intent(QuizActivity.this, ResultsActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt("finalScore", mScore);
                        i.putExtras(bundle);
                        QuizActivity.this.finish();
                        startActivity(i);
                    } else {
                        updateQuestion();
                    }
                }
            }
        });

    }

    private void updateQuestion() {
        shuffleQuizbook();
        mImageView.setImageResource(questionArray[0].getmImages());
        mQuestion.setText(questionArray[0].getmQuestions());
        mAnswer = questionArray[0].getmAnswers();
        mQuestionNumber++;
    }
    QuizBook q0 = new QuizBook(R.drawable.ant,"Ants lives in a colony",true);
    QuizBook q1 = new QuizBook(R.drawable.bird,"Bird can fly",true);
    QuizBook q2 = new QuizBook(R.drawable.camel,"Camel can survive in a dessert",true);
    QuizBook q3 = new QuizBook(R.drawable.cow,"Cows laid eggs",false);
    QuizBook q4 = new QuizBook(R.drawable.crocodile,"Crocodiles are mammals",false);
    QuizBook q5 = new QuizBook(R.drawable.dog,"Dogs has excellent sniffing ability",true);
    QuizBook q6 = new QuizBook(R.drawable.duck,"Ducks able to swim",true);
    QuizBook q7 = new QuizBook(R.drawable.elephant,"Elephants has a wide ears",true);
    QuizBook q8 = new QuizBook(R.drawable.giraffe,"Girrafe has a short neck",false);
    QuizBook q9= new QuizBook(R.drawable.hippopotamus,"Hippo's average weights are <1000kgs",false);
    QuizBook q10 = new QuizBook(R.drawable.horse,"Horses has four legs",true);
    QuizBook q11 = new QuizBook(R.drawable.kangaroo,"Kangaroos has a pocket in their belly",true);
    QuizBook q12= new QuizBook(R.drawable.lion,"Only male lion has long hair around their neck",true);
    QuizBook q13= new QuizBook(R.drawable.monkey,"Monkey is a carnivore",false);
    QuizBook q14= new QuizBook(R.drawable.mouse,"Mouse is an omnivore",true);
    QuizBook q15 = new QuizBook(R.drawable.pig,"Pigs only has 4 milk udders",false);
    QuizBook q16= new QuizBook(R.drawable.rabbit,"Rabbits has long ears",true);
    QuizBook q17= new QuizBook(R.drawable.rhino,"There is no one-horned rhino",false);
    QuizBook q18= new QuizBook(R.drawable.rooster,"Rooster laid eggs",false);
    QuizBook q19= new QuizBook(R.drawable.shark,"Shark is not a fish",true);
    QuizBook q20= new QuizBook(R.drawable.sheep,"Sheep is a carnivore",false);
    QuizBook q21= new QuizBook(R.drawable.snail,"Snails moves very fast",false);
    QuizBook q22= new QuizBook(R.drawable.snake,"Snake doesn't have any legs",true);
    QuizBook q23= new QuizBook(R.drawable.tiger,"Only tiger's furs are stripped",false);
    QuizBook q24= new QuizBook(R.drawable.whale,"Whale doesn't need to resurface to breathe",false);




    QuizBook [] questionArray = new QuizBook[]{q0,q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12,q13,q14,q15,q16,q17,q18,q19,q20,q21,q22,q23,q24};
    public void shuffleQuizbook() {
        Collections.shuffle(Arrays.asList(questionArray));
    }



    private void updateScore(int point) {
        mScoreView.setText("" + mScore);
    }

    public void clickExit(View view) {
        askToClose();
    }


    @Override
    public void onBackPressed() {
        askToClose();
    }

    private void askToClose (){
        AlertDialog.Builder builder = new AlertDialog.Builder(QuizActivity.this);
        builder.setMessage("Are you sure you want to quit?");
        builder.setCancelable(true);
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                finish();
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();

            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
}

我将使用lcm的输出从上面的列表中随机播放数组

如果有人告诉我如何

,我真的很感激

0 个答案:

没有答案