1부터 37까지 6 개의 고유 난수를 생성해야합니다. 처음에는 간단한 배열 매핑을 사용 :비 균일 랜덤 화자
문제는 (내가 열 적은 다음 그립니다하게 escpicially 경우) 많은 경우에 나는 근처 uniformic 분포를 가지고 있다고했다private int k=6, n=37;
public int[] Results()
{
// fill an array with numbers 1 2 3 . . . n
int[] numbers = new int[n];
for (int i = 0; i < numbers.length; i++)
numbers[i] = i + 1;
// draw k numbers and put them into a second array
int result[] = new int[k];
for (int i = 0; i < result.length; i++)
{
// make a random index between 0 and n - 1
int r = (int) (Math.random() * n);
// pick the element at the random location
result[i] = numbers[r];
// move the last element into the random location
numbers[r] = numbers[n - 1];
n--;
}
return result;
}
예 : 1,9,16, 18,24,30 또는 5,16,18,22,26,29
실제 필요한 결과는 다음과 같습니다. 11,16,25,29,30,32 또는 4 , 8,9,15,18,19 그 다음에 10 그립니다.
import java.util.*;
public class RandomHash
{
HashMap numbers = new HashMap() ;
Random rnd_gen = new Random() ;
RandomHash()
{
for(;;)
{
int rnd_num = rnd_gen.nextInt() ;
Integer rnd_num_obj = new Integer(rnd_num) ;
if (! numbers.containsKey(rnd_num_obj))
{
numbers.put(rnd_num_obj, rnd_num_obj) ;
/* Do whatever with the number */
break ;
} /* else loop and get another rnadom num */
} /*end for*/
}
}
문제는 내가 현재 각각 6, 32로 랜덤과 해시 맵을 결합하는 방법을 모르겠입니다 :
는 또한 비슷한의 HashMap의 구현을 보았다. 해시 맵이 더 많은 결과를 얻을 수 있습니까?
은 "TRUE 무작위로"숫자를 정의하고 당신이 얻을 출력이 진정으로 무작위되지 않은 이유를 보여줍니다. –
"비 균일"이란 무엇입니까? "비 유니폼"이라는 뜻입니까? 대신에 어떤 배포판이 필요합니까? –