logo

Java의 Java.util.Random.nextInt()

난수를 생성하는 것 자체는 좋은 효용 가치를 가지며 함수를 사용하여 이를 달성하는 것은 매우 유용할 수 있습니다. Java는 일상적인 프로그래밍에서 그 중요성을 확인하기 위해 전체 라이브러리를 난수 전용으로 사용했습니다. nextInt()는 이 문서에서 논의됩니다.

    java.util.Random.nextInt() : nextInt() 이 난수 생성기 시퀀스에서 다음 난수 정수 값을 얻는 데 사용됩니다.

     Declaration :  public int nextInt() Parameters :  NA Return Value :  The method call returns the next integer number from the sequence Exception :  NA>

    다음 예에서는 java.util.Random.nextInt()의 사용법을 보여줍니다.




    자바의 반환 유형



    // Java code to demonstrate the working> // of nextInt()> import> java.util.*;> public> class> NextInt1 {> >public> static> void> main(String[] args)> >{> > >// create random object> >Random ran =>new> Random();> > >// generating integer> >int> nxt = ran.nextInt();> > >// Printing the random Number> >System.out.println> >(>'The Randomly generated integer is : '> + nxt);> >}> }>

    >

    >

    산출:

     The Randomly generated integer is : -2052834321>
    java.util.Random.nextInt(int n) : nextInt(int n) 0(포함)과 이 인수(n)에 전달된 숫자(제외) 사이의 난수를 얻는 데 사용됩니다.

     Declaration :  public int nextInt(int n) Parameters :  n :  This is the bound on the random number to be returned. Must be positive. Return Value :  Returns a random number. between 0 (inclusive) and n (exclusive). Exception :  IllegalArgumentException : This is thrown if n is not positive.>

    다음 예에서는 java.util.Random.nextInt(int n)의 사용법을 보여줍니다.




    // Java code to demonstrate the working> // of nextInt(n)> import> java.util.*;> public> class> NextInt2 {> >public> static> void> main(String args[])> >{> > >// create random object> >Random ran =>new> Random();> > >// Print next int value> >// Returns number between 0-9> >int> nxt = ran.nextInt(>10>);> > >// Printing the random number> >// between 0 and 9> >System.out.println> >(>'Random number between 0 and 10 is : '> + nxt);> >}> }>

    >

    >

    산출:

     Random number between 0 and 9 is : 4>
예외

IllegalArgumentException: 이는 전달된 인수가 양수가 아닐 때 발생합니다.
n이 양수가 아닐 때 생성되는 예외를 설명하는 예:


비교 가능한 목록



// Java code to demonstrate the Exception> // of nextInt(n)> import> java.util.*;> public> class> NextInt2 {> >public> static> void> main(String[] args)> >{> > >// create random object> >Random ran =>new> Random();> > >// generating number between 0 and -12345> >// Raises Runtime error, as n is negative.> >int> nxt = ran.nextInt(->12345>);> > >System.out.println> >(>'Generated Random number is : '> + nxt);> >}> }>

>

>

런타임 오류:

 Exception in thread 'main' java.lang.IllegalArgumentException: bound must be positive at java.util.Random.nextInt(Random.java:388) at NextInt2.main(NextInt2.java:14)>

실용적인 적용

난수 생성에는 수많은 응용 프로그램이 있습니다. 복권, 도박 또는 소규모 게임 . 아래에서는 2명의 플레이어가 6점 주사위를 던져 30점을 먼저 확보하는 사람이 승리하는 작은 주사위 게임을 보여줍니다.




// Java code to demonstrate the Application> // of nextInt(n)> import> java.util.*;> public> class> NextIntAppli {> >public> static> void> main(String[] args)> >{> > >int> sum =>0>, sum1 =>0>, count =>0>, count1 =>0>;> >int> turn =>0>;> > >// creating random object> >Random ran =>new> Random();> >int> flag =>0>;> > >while> (>true>) {> >if> (turn %>2> ==>0>) {> > >int> p1 = ran.nextInt(>6>);> >sum += p1;> >System.out.printf> >(>'Player 1 after turn %d is : %d '>, turn, sum);> >}> >else> {> > >int> p2 = ran.nextInt(>6>);> >sum1 += p2;> >System.out.printf> >(>'Player 2 after turn %d is : %d '>, turn, sum1);> >}> >if> (sum>=>30>) {> >flag =>1>;> >break>;> >}> >if> (sum1>=>30>) {> >flag =>2>;> >break>;> >}> >turn++;> >}> >if> (flag ==>1>)> >System.out.println(>' Player 1 WON!!'>);> >else> >System.out.println(>' Player 2 WON!!'>);> >}> }>

>

>

산출:

 Player 1 after turn 0 is : 0 Player 2 after turn 1 is : 4 Player 1 after turn 2 is : 2 Player 2 after turn 3 is : 9 Player 1 after turn 4 is : 5 Player 2 after turn 5 is : 9 Player 1 after turn 6 is : 6 Player 2 after turn 7 is : 14 Player 1 after turn 8 is : 8 Player 2 after turn 9 is : 18 Player 1 after turn 10 is : 12 Player 2 after turn 11 is : 21 Player 1 after turn 12 is : 13 Player 2 after turn 13 is : 26 Player 1 after turn 14 is : 18 Player 2 after turn 15 is : 29 Player 1 after turn 16 is : 18 Player 2 after turn 17 is : 34 Player 2 WON!!>