Linked Questions

Popular Questions

How to prevent numbers from reappearing with math.random?

Asked by At

I'm trying to make a code to design tournament match-ups in a random fashion. I'm trying to make it so that the numbers I randomly generate in the "for" statement to identify the participants do not reappear. I've tried looking at other posts for suggestions but I have not been able to implement the solutions they offered successfully. Any help is appreciated. Thank you.

public class Tournament {
public static void main (String[]args) {
int[] arr= new int[56];

for(int i=0; i<arr.length; i++) {
    arr[i]= i;
}
for(int k=0;k<arr.length;k+=2) {
    int p=(int) (Math.random()*((55-1)+1)+1);
    int r=(int) (Math.random()*((55-1)+1)+1);
    arr[k]=p;
    arr[k+1]=r;
    if  (arr[k]!=arr[k+1]) {
    System.out.println(+arr[k]+" vs "+arr[k+1]);
    }}}}

Related Questions