[Java] Class Random

hameeeΒ·2023λ…„ 9μ›” 26일
1

Java

λͺ©λ‘ 보기
11/26
post-thumbnail

πŸ“ Class Random

  • λ‚œμˆ˜λ₯Ό μƒμ„±ν•˜λŠ” ν΄λž˜μŠ€μ΄λ‹€.
  • integer, float, double, long, boolean νƒ€μž…μ˜ λ‚œμˆ˜λ₯Ό 생성할 수 μžˆλ‹€.
  • λ©”μ†Œλ“œμ— 인수λ₯Ό μ „λ‹¬ν•˜μ—¬ λ‚œμˆ˜μ˜ μƒν•œμ„ 지정할 수 μžˆλ‹€.
package lesson06;

import java.util.Random;

public class Quiz03 {

	public static void main(String[] args) {
		Random rand = new Random();

		// integer
		System.out.println(rand.nextInt()); // μ •μˆ˜ 전체 λ²”μœ„μ˜ λ‚œμˆ˜
		System.out.println(rand.nextInt(10)); // 0λΆ€ν„° 10 미만의 λ‚œμˆ˜
		
		// double
		System.out.println(rand.nextDouble()); // μ‹€μˆ˜ 전체 λ²”μœ„μ˜ λ‚œμˆ˜
		System.out.println(rand.nextDouble(0.5)); // 0.0λΆ€ν„° 0.5 미만의 λ‚œμˆ˜
        
		// boolean
		System.out.println(rand.nextBoolean()); // true λ˜λŠ” false
	}

}

References

πŸ”— https://docs.oracle.com/javase/8/docs/api/java/util/Random.html
πŸ”— https://www.geeksforgeeks.org/generating-random-numbers-in-java

0개의 λŒ“κΈ€