Monday, July 27, 2015

// //

Random number generator in a specific range

This time we will have to generate a random number in an specific range..

by using:
java.util.Random;

RandomNumber.java

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.TextView;

public class RandomNumber extends Activity {
 //not lower than 65 and not greater than 80
 int min = 65;
 int max = 80;
/** Called when the activity is first created. */
             @Override
             public void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.main);

                      final  Random r = new Random();

                        Button buttonGenerate = (Button)findViewById(R.id.generate);
                        final TextView textGenerateNumber = (TextView)findViewById(R.id.generatenumber);
                        final TextView textGenerateNumber2 = (TextView)findViewById(R.id.generatenumber2);
                        
                       
                        
                        
                        buttonGenerate.setOnClickListener(new OnClickListener(){

                                 @Override
                                 public void onClick(View v) {
                                      // TODO Auto-generated method stub
                                  textGenerateNumber.setText(String.valueOf(r.nextInt(max - min + 1)+ min));
                                     textGenerateNumber2.setText(String.valueOf(r.nextInt(max - min + 1)+ min));
                                  }
                      });
       }
}



Main.xml



Now test in your emulator.