주어진 값 사이에 두 난수를 생성할 수 있는 방법을 알고 싶어요.
나는 난수를 생성할 수 있는 다음과 같습니다.
Random r = new Random();
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[i].length; j++){
a[i][j] = r.nextInt();
}
}
그러나 0-사이의 100mb/s (포함) 난수를 생성할 수 있습니까?
예를 들어 '레지널드네스틴트 (101)' 을 사용할 수 있습니다
좀 더 일반적인 경우, 둘 사이에 있는 " numbers". 사용:
Random r = new Random();
int low = 10;
int high = 100;
int result = r.nextInt(high-low) + low;
따라서 너희에의 난수 사이에 10 (포함) 및 100mb/s (독점)
위 아래, 그러면 바인딩되지 쇱そ 는 가정 및 낮을수록 바인딩될 내말들어봐 난수를, r, 둘 사이의 니며 호스트당:
int r = (int) (Math.random() * (upper - lower)) + lower;
int Random = (int)(Math.random()*100);
발령합니다 할 경우, 다음 두 개 이상의 가치를 됩니다 이를 위해 for 루프
for (int i = 1; i <= 10 ; i++)
{
int Random = (int)(Math.random()*100);
System.out.println(Random);
}
좀 더 괜찮은 10 개에서 100 범위를 지정할 경우 같은 (모두 범위의)
그래서 이 코드는 다음과 같습니다.
int Random =10 + (int)(Math.random()*(91));
/* int Random = (min.value ) + (int)(Math.random()* ( Max - Min + 1));
*Where min is the smallest value You want to be the smallest number possible to
generate and Max is the biggest possible number to generate*/
package RandGen;
import java.util.Random;
public class RandGen {
public static Random numGen =new Random();
public static int RandNum(){
int rand = Math.abs((100)+numGen.nextInt(100));
return rand;
}
public static void main(String[]Args){
System.out.println(RandNum());
}
}
S # 39 의 기능을 완전히 거짓한 program& 6 호선, 이 중, int rand." " 시작으로. 참고로 마테오바스 () 는 단순히 절대값입니다 및 it& # 39 명으로, s, s # 39 표시됨과 int, 정말 중요한 것은 that& 선언했다. 첫 번째 (100) 은 추가 번호임 실로나는 무순서 하나. 즉, 임의의 번호 + 100. 누마겐스네스틴트 () 는 새로운 출력입니다 수치가 난수 값 때문에 자체에 I put it 는 임의의 숫자 1 부터 100 (100), 괄호 안에. 내가 100, 200 101 과 사이의 숫자를 외면하였나니 추가 됩니다. # 39 는 100m 와 200m 사이의 숫자를 생성, 실제로 너회가 aren& 추가하는 1, 1 부터 100.
또한 한 시도하시겠습니까 다음과 같습니다.
public class RandomInt {
public static void main(String[] args) {
int n1 = Integer.parseInt(args[0]);
int n2 = Integer.parseInt(args[1]);
double Random;
if (n1 != n2)
{
if (n1 > n2)
{
Random = n2 + (Math.random() * (n1 - n2));
System.out.println("Your random number is: " + Random);
}
else
{
Random = n1 + (Math.random() * (n2 - n1));
System.out.println("Your random number is: " +Random);
}
} else {
System.out.println("Please provide valid Range " +n1+ " " +n2+ " are equal numbers." );
}
}
}