0

I'm quite new to Android so please don't shoot me for asking a dumb question. I've already checked 'Stackoverflow' and many other sites but can't seem to find the right answer...

I'm creating a simple math app to help my daughter with her math. While I try to create two random numbers in, let's say the multiplication table of 1, they always start with the two same numbers (8x7). After I click my submit button though, I get the 'right' numbers which means that the first number is '1' and the second number is a number between '0' and '11'.

Any help is appreciated. Thanks!

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class Vermenigvuldig extends Activity implements OnClickListener {

TextView firstNumber;
TextView secondNumber;
TextView times_sign, equals_sign, ok;
EditText answer;
Button submit;
int solution;

// int firstNumberB, secondNumberB;
ImageView thumbs;

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

    firstNumber = (TextView) findViewById(R.id.tv_fnum);
    secondNumber = (TextView) findViewById(R.id.tv_snum);
    times_sign = (TextView) findViewById(R.id.tv_x);
    answer = (EditText) findViewById(R.id.et_answer);
    submit = (Button) findViewById(R.id.b_submit);
    submit.setOnClickListener(this);
    thumbs = (ImageView) findViewById(R.id.iv_thumbs);

}

@Override
public void onClick(View v) {

    Random myRandom = new Random();
    switch (v.getId()) {
    case R.id.b_submit:

        int test1 = Integer.parseInt(firstNumber.getText().toString());
        int test2 = Integer.parseInt(secondNumber.getText().toString());

        solution = test1 * test2;

        firstNumber.setText(String.valueOf(myRandom.nextInt(10)));
        secondNumber.setText(String.valueOf(myRandom.nextInt(10)));
        int outcome = Integer.parseInt(answer.getText().toString());
        if (outcome == solution) {
            thumbs.setImageResource(R.drawable.thumbsup);
        } else {
            thumbs.setImageResource(R.drawable.thumbsdown);
        }

        answer.setText("");
        break;
    }
}
} 
  • have you looked here: http://stackoverflow.com/questions/5533191/java-random-always-returns-the-same-number-when-i-set-the-seed ? – yossico Jul 02 '14 at 07:03

2 Answers2

0

You are plagued by the nightmare of all testers. Test a random number generator. There is no way to test the randomness of any algorithm. I suggest that you generate a larger random number and mod it to get a number that is random enough. The smaller the sample the lesser random your algo will be, nothing much you can do about it. Its mathematics.

Hope I have hinted you towards a answer.

Siddharth
  • 9,349
  • 16
  • 86
  • 148
0

You can make a kind of not so clean solution. Check the values of random and if they are the same numbers as before, start random again. This theoretically could hang up in a loop for a while if the random is allways the same. But I think, in this small app, it should be no problem. For example, start with making the integer values global:

   private int valueOneOldValue=0;
   private int valueTwoOldValue=0;
   private Random myRandom = new Random();

then separate all in in it´s own method:

   private int generateOutcome{

       int out = myRandom.nextInt(10);
       return out;
   }

then check if it is the same value as before:

   private boolean isEqual(int oldValue, int newValue){

        boolean equal=false;

     if(oldValue==newValue){
           equal=true;
     }else{
           equal=false;
     }   
    return equal;
    }

And then do the rest of the stuff:

  private void generateExercise(){         

    int firstValue = generateOutcome();
    int secondValue = generateOutcome();

    if(isEqual(valueOneOldValue,firstValue)==true){

     generateExercise();

    }else{

     if(isEqual(valueTwoOldValue,secondValue)==true){

        generateExercise();
      }else{

          setValues(firstValue,secondValue);
      }

    }
  }

and set your values if all is ok:

    private void setValues(int first, int second){

     int test1 = Integer.parseInt(firstNumber.getText().toString());
    int test2 = Integer.parseInt(secondNumber.getText().toString());

    solution = test1 * test2;

     firstNumber.setText(String.valueOf(first));
    secondNumber.setText(String.valueOf(second));
    int outcome = Integer.parseInt(answer.getText().toString());
    if (outcome == solution) {
        thumbs.setImageResource(R.drawable.thumbsup);
    } else {
        thumbs.setImageResource(R.drawable.thumbsdown);
    }

    answer.setText("");
    valueOneOldValue = first;
    valueTwoOldValue = second;

   }

and call just this onButtonClick:

       generateExercise();

This is just an idea and not tested for now. If You think it could be a solution of Your problem but You got mor questions or any error for that code (because I can´t test it now), let me know.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49