gidrokolbaska gidrokolbaska - 3 months ago 4
Java Question

How do I make one random generated number equal to another?

I have two different random number generators: "numtofind" and "num". The first one puts a number from 1 to 50 into a

textView("textNumberToHit")
and the second one puts a number from 1 to 50 to another ten
textViews("textGeneratenumber1-12")
.

When the game starts, I want the first
textView("textNumberToHit")
to be contained in one of the other
textView("textGeneratenumber")
elements. That is, if
textView("textNumberToHit")
is 17, 17 should appear in one of the other elements. I want that to happen when a "buttonGenerate" button is pressed.

While I can press the button, I am having trouble finding a way of doing the above logic.

Here is the code for textNumberToHit:

final Random numtofind = new Random();
final TextView textNumberToHit = (TextView) findViewById(R.id.numbertofind);
textNumberToHit.setText(String.valueOf(numtofind.nextInt(51)));


Here is the code for textGeneratenumber:

final Random num = new Random();
buttongenerate.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:

buttongenerate.setBackgroundResource(R.drawable.pressedbut);
textGenerateNumber1.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber2.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber3.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber4.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber5.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber6.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber7.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber8.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber9.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber10.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber11.setText(String.valueOf(num.nextInt(51)));
textGenerateNumber12.setText(String.valueOf(num.nextInt(51)));
break;
case MotionEvent.ACTION_UP:

buttongenerate.setBackgroundResource(R.drawable.normalbut);
break;
}
return true;
}
});


Here is a picture for you to better understand:
example

How would I accomplish this?

Answer

Something I would recommend to begin with is rather than having 12 static TextView's to use a GridView: https://developer.android.com/guide/topics/ui/layout/gridview.html

To Answer your question with what you have though; This is how i would approach the problem:

Steps:

  1. Save the value of numtofind.nextInt(51) in a variable.
  2. Create another random number generator that will determine which of the 12 TextViews to insert that stored value into. Then insert that value in that TextView.
  3. Insert random numbers in the remaining textviews.

Code:

final Random numtofind = new Random();
final int numtofindValue = numtofind.nextInt(51);
final TextView textNumberToHit = (TextView) findViewById(R.id.numbertofind);
    textNumberToHit.setText(String.valueOf(numtofindValue));

final Random num = new Random();
final Random textViewToSet = new Random();

// reason i am adding 1 is because nextInt(int num) returns a random number from 0 up to but not including the value of the param "num" 
// http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#nextInt(int)
// and since you have 12 textviews and we want to get one of those we need to add one so the range of random numbers will be 1-12 instead of 0-11.
int textViewToSetValue = textViewToSet.nextInt(12) + 1; // which textview to set the value of numtofind

// populate hashmap with textviews
Map<Integer, TextView> textViews = new HashMap<>();

// not an efficient way of doing this - but since you are not using a GridView its the only way
textViews.put(1, textGenerateNumber1);
textViews.put(2, textGenerateNumber2);
textViews.put(3, textGenerateNumber3);
textViews.put(4, textGenerateNumber4);
textViews.put(5, textGenerateNumber5);
textViews.put(6, textGenerateNumber6);
textViews.put(7, textGenerateNumber7);
textViews.put(8, textGenerateNumber8);
textViews.put(9, textGenerateNumber9);
textViews.put(10, textGenerateNumber10);
textViews.put(11, textGenerateNumber11);
textViews.put(12, textGenerateNumber12);

TextView tvToSet = textViews.get(textViewToSetValue); // this will get the textview for a number 1 - 12

// set value of textView 
tvToSet.setText(String.valueOf(numtofindValue));

buttongenerate.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:

                    buttongenerate.setBackgroundResource(R.drawable.pressedbut);
                    // loop through the remaining textViews and set their values to random values
                    for (Map.Entry<Integer, TextView> entry : textViews.entrySet(){
                        // make sure we dont reset the value we set earlier
                        if(entry.getKey() != textViewToSetValue){
                            entry.getValue().setText(num.nextInt(51));
                        }
                    }

                    break;
                case MotionEvent.ACTION_UP:

                    buttongenerate.setBackgroundResource(R.drawable.normalbut);
                    break;
            }
            return true;
        }
});
Comments