안드로이드 자바

[Java][Android] 랜덤 Random 클래스의 다양한 활용법

teamnova 2025. 5. 20. 14:58
728x90

안녕하세요

오늘은 Random 클래스의 다양한 활용법에 대해 알아보도록 하겠습니다.

 

전체 코드입니다.

 

MainActivity.java

public class MainActivity extends AppCompatActivity {

  private TextView resultText;
  private final Random random = new Random();
  private final List<String> fruits = Arrays.asList("apple", "banana", "orange", "grape");
  private final List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

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

    resultText = findViewById(R.id.resultText);

    findViewById(R.id.btnRandomInt).setOnClickListener(v -> {
      int value = random.nextInt(100);  // 0 ~ 99
      resultText.setText("랜덤 정수: " + value);
    });

    findViewById(R.id.btnRandomFloat).setOnClickListener(v -> {
      double value = random.nextDouble();  // 0.0 ~ 1.0
      resultText.setText("랜덤 실수: " + value);
    });

    findViewById(R.id.btnRandomBoolean).setOnClickListener(v -> {
      boolean value = random.nextBoolean();
      resultText.setText("랜덤 불리언: " + value);
    });

    findViewById(R.id.btnRandomList).setOnClickListener(v -> {
      String value = fruits.get(random.nextInt(fruits.size()));
      resultText.setText("랜덤 과일: " + value);
    });

    findViewById(R.id.btnShuffleList).setOnClickListener(v -> {
      List<Integer> shuffled = new ArrayList<>(numbers); // 원본 보존
      Collections.shuffle(shuffled);
      resultText.setText("섞은 리스트: " + shuffled);
    });
  }
}

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <TextView
      android:id="@+id/resultText"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="여기에 결과가 표시됩니다."
      android:textSize="16sp"
      android:paddingBottom="16dp"/>

    <Button android:id="@+id/btnRandomInt"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="랜덤 정수 생성 (0~99)" />

    <Button android:id="@+id/btnRandomFloat"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="랜덤 실수 생성 (0.0~1.0)" />

    <Button android:id="@+id/btnRandomBoolean"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="랜덤 불리언 (true/false)" />

    <Button android:id="@+id/btnRandomList"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="리스트에서 랜덤 선택" />

    <Button android:id="@+id/btnShuffleList"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="리스트 섞기" />
  </LinearLayout>
</ScrollView>

 

이처럼 Random 클래스의 메소드를 활용하면 다양한 형태로 구현이 가능합니다.

 

시연 영상입니다.