user3052605
user3052605

Reputation:

Control random numbers

How do you control what random numbers you get. for example how would you control the random number generator from numbers 10 - 20. I am using eclipse here is my code:

public class FrontPage extends Activity {
Random rndNumbers = new Random(); 
int d20    = rndNumbers.nextInt(20) +1; 

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



            Button add = (Button) findViewById(R.id.button1);
            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    int d20    = rndNumbers.nextInt(20) +1; 
                    EditText comment = (EditText) findViewById(R.id.editText1); 
                    Editable commy = comment.getText() ;
                    String  str =   commy.toString(); 
        TextView TV = (TextView) findViewById(R.id.textView1);
        TV.setText("" + str + d20);
        TV.setTextSize(d20);

                }
            });



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.front_page, menu);
    return true;
}

}

Please help.

Upvotes: 1

Views: 995

Answers (1)

Alexis C.
Alexis C.

Reputation: 93862

how would you control the random number generator from numbers 10 - 20

Create a random number between 0 and 10 and add 10 to it.

int d20 = rndNumbers.nextInt(11)+10; 

Upvotes: 7

Related Questions