Basically, I need to generate 5 random numbers that are unique every time they are generated (so they don't repeat) and then output them to a TextView.
I managed to generate the 5 numbers, however, they are not unique all the time, meaning sometimes the same number shows up twice or more when the button is pressed to generate those numbers.
Here's what I have gotten so far:
import androidx.annotation.IntegerRes;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private TextView numbersGenerated;
private Button btnGenerate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numbersGenerated = findViewById(R.id.numbers);
btnGenerate = findViewById(R.id.btnGenerate);
btnGenerate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<Integer> arrayRandom = new ArrayList<>();
Random rand = new Random();
for (int i = 0; i < 5; i++)
{
Integer r = rand.nextInt(50) + 1;
arrayRandom.add(r);
}
numbersGenerated.setText(arrayRandom.toString());
}
});
}
}
I am not sure what type of validation I can do since this is my first ever contact with Java, so any help is appreciated.