-3

I'm new to the Android world, I'm doing an application-style Simon. but I'm stuck, I'm not able to create a code that creates random numbers to match the start button.

Could someone help me please?

thx

PiBi
  • 39
  • 1
  • 1
  • 1
    possible duplicate of [Generating random integers in a range with Java](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java) – Scherling Jan 28 '15 at 08:53
  • 1
    Android is written in Java. If you have basic programming questions that dosen't directly relate to the mobile device or the UI, there's a very good chance you can find the answer on StackOverflow if you search for "Java" rather than "Android". – Scherling Jan 28 '15 at 08:59

3 Answers3

5

Try this. Returns an int in the half-open range (0 to 100)

Random rand = new Random();
int abcd = rand.nextInt(100);
Hemanth
  • 2,717
  • 2
  • 21
  • 29
0

Adding to Hemanth answer,

Random r = new Random();
int i1 = r.nextInt(45 - 28) + 28;

This gives a random integer between 28 (inclusive) and 45 (exclusive), one of 28,29,...,43,44.

Jayavinoth
  • 544
  • 1
  • 9
  • 24
-1

I would generate a number from 1 to 4, to be inserted in the code part of the start button.

edit the code where I should add

public class MainActivity extends Activity {

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

    Button btnStart= (Button)findViewById(R.id.status);
    btnStart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Here I should generate the random code
            // I created the toast to see if it worked
            Toast.makeText(MainActivity.this, " start click", Toast.LENGTH_LONG).show();

        }


    });
PiBi
  • 39
  • 1
  • 1