0

I made an offline quiz game in which I ask 10 random questions to user. These questions are taken from my firebase db and kept in a arraylist. My program picks a random question from the arraylist each time and display the question. Here is a piece of my code

 public void askQuestion(){
        Random rnd=new Random();
        int index = rnd.nextInt(questionList.size());
        Info theQuestion=questionList.get(index);  
        question.setText(theQuestion.getQuestion());
        a.setText(theQuestion.getA());
        b.setText(theQuestion.getB());
        c.setText(theQuestion.getC());
        d.setText(theQuestion.getD());
        answer=theQuestion.getAnswer();
    }
//Info is the name of the object for my questions. questionList is an arraylist of type info where I keep the all questions I got from firebase.

Here is my problem(s).

  1. I read that I should use google play services to make a game online. Is there a better approach? What is the best place to start(a link would be appreciated)
  2. Can I use this activity in my online game or should I change it? Will randomness be same in both users? I want to ask them same questions.
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Freshia
  • 17
  • 4

1 Answers1

0

Will randomness be same in both users?

From the Android documentation on the Random class:

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.

So just ensure that you create your Random object with an initializer that is the same between players:

Random rnd=new Random(42);

To increase replayability of your game, you could seed the randomizer with a changing-but-predictable value. For example: a simlpe way to implement a game-of-the-day is to seed it with the hash code of the day:

Calendar c = new GregorianCalendar(); // see http://stackoverflow.com/q/6850874
c.set(Calendar.HOUR_OF_DAY, 0); //anything 0 - 23
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
Date day = c.getTime(); //the midnight, that's the first second of the day.
Random rnd=new Random(day.hashCode());
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807