简体   繁体   中英

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: 例

How would I accomplish this?

Think about it this way. Here is a (simplified) procedure based on what you are asking.

  1. I generate a random number and it is 17.
  2. I generate a second random number and it must be 17.

Q: What is the logical / mathematical flaw in the description of that procedure?
A: Clearly the second number is NOT a random number!

So here's what you need to do. (Simplified version!)

  1. Generate 12 different 1 random numbers and assign them to the 12 buttons.
  2. Generate a random button index between 1 and 12.
  3. Get the value of that button, and assign it to the "number to find" text box.

.... or something like that.


1 - At least, I assume they need to be different. Your game is going to look rather odd if you occasionally see the "number to be found" on multiple buttons ....

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

The first thing is, you want to store the value of generated number to hit:

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

Now, you are generating 12 other random numbers for your textGenerateNumber. There are following problems with it:

  1. Two or more numbers (wrong ones) could be repeated.
  2. Two or more correct numbers could be repeated.

So, first I would create 12 unique randoms.

ArrayList<Integer> wrongNumbers = new ArrayList<Integer>();
int wrongNumber = 0;

//I will not write the exact code. You will need to do them but the logic is correct.

for(loop from 1 to 50) //you would not be looping 50 times. It is very unlikely that you won't get 12 random numbers even in 50 times (worst case)
  {
  wrongNumber = numtofind.nextInt(51);
  if(wrongNumber DOES NOT EXIST IN wrongNumbers)
   {
    wrongNumber => (PUSH TO ARRAY(wrongNumbers))
    }

   if(wrongNumbers.SIZE => 12) //You know have 12 numbers
       {exit from this for loop;}
}


//Now create the textGenerateNumbers in loop.

buttongenerate.setBackgroundResource(R.drawable.pressedbut);
for(loop from int i = 0 => 11)
{
   textGenerateNumber[i + 1].setText(wrongNumbers[i]); //as your textGenerateNumber is from 1-12
}

//Create a random value from 0-11 to hold the correct value.

int correctIndex = numtofind.nextInt(11);

//Finally, override the value at the correctIndex with your number to hit.

textGenerateNumber[correctIndex] = generatedNumber; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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