3 Replies - 226 Views - Last Post: 16 May 2014 - 04:07 AM Rate Topic: -----

#1 armonsafai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-May 14

Trying to assign random numbers to a deck of cards

Posted 15 May 2014 - 07:22 PM

Im trying to assign random numbers to a deck of cards without repeating the numbers. What am I doing wrong?


package hokm;

import java.util.Random;
import java.util.Scanner;

public class Hokm 
{
    public static void main(String[] args)
    {
        int [][] number=new int[52][2];       
        
        Random random=new Random();

        int [] array= new int[53];
        
       for(int i=0;i<number.length;i++)
       {
           int Number=random.nextInt(52)+1;         //shuffle method          
                    
           array[Number]++;
               
           while(array[Number]>1)
           {
               Number=random.nextInt(52)+1;
           }
           
           array[Number]++;
             
           number[i][0]=Number;
       }     
       
      
       
    }
    
}

This post has been edited by Flukeshot: 15 May 2014 - 10:15 PM
Reason for edit:: code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to assign random numbers to a deck of cards

#2 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Trying to assign random numbers to a deck of cards

Posted 15 May 2014 - 10:35 PM

You seriously need to considder better variable names than this..

Random is probably going to be spitting out duplicate numbers. Try something like this:

import java.util.Random;

public class Hokm {
	
	Random r = new Random();
	int[] usedNumbers = new int[52];
	
	void generate52UniqueRandomNumbers() {
		
		for(int i = 0; i < usedNumbers.length; i++) { // iterate over the array index
			
			int thisPass = r.nextInt(52)+1; // create a random number
			
			for(int j : usedNumbers) { // iterate over the array
				
				if(j==thisPass) continue; // check this random number is not already present
			}
			
			usedNumbers[i] = thisPass; // we will only reach this line if we have a unique number
		}
	}
	
	public static void main(String[] args) {
		
		new Hokm().generate52UniqueRandomNumbers();
	}
}

Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2727
  • View blog
  • Posts: 11,468
  • Joined: 20-September 08

Re: Trying to assign random numbers to a deck of cards

Posted 16 May 2014 - 12:42 AM

Quote

Im trying to assign random numbers to a deck of cards without repeating the numbers.
Why? And why are you allotting storage for two numbers per 'card'?

Quote

int [][] number=new int[52][2];

Was This Post Helpful? 0
  • +
  • -

#4 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Trying to assign random numbers to a deck of cards

Posted 16 May 2014 - 04:07 AM

You could implement a random index generator using the Fisher-Yates shuffle algorithm which will shuffle the array in place. [Live Demo]

import java.util.Arrays;
import java.util.Random;

class ShuffleIndicies {
	public static void main (String[] args) {
		int[] indicies = range(52);
		print("Before:", indicies);
		shuffleArray(indicies);
		print("After:", indicies);
	}
	
	// Fisher–Yates shuffle
	public static void shuffleArray(int[] arr) {
		Random r = new Random();
		for (int i = arr.length - 1; i > 0; i--) {
			int index = r.nextInt(i + 1);
			int t = arr[index];
			arr[index] = arr[i];
			arr[i] = t;
		}
	}
	
	public static final int[] range(int length) {
		return range(0, length);
	}
	
	public static final int[] range(int start, int length) {
		int[] range = new int[length - start + 1];
		for (int i = start; i <= length; i++) {
			range[i - start] = i;
		}
		return range;
	}
	
	public static void print(String label, int[] arr) {
		System.out.printf("%s\n%s\n", label, Arrays.toString(arr));
	}
}


Output said:

Before:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52]
After:
[6, 39, 23, 4, 50, 47, 33, 48, 1, 30, 40, 2, 22, 42, 41, 16, 13, 24, 12, 5, 18, 19, 35, 7, 9, 52, 31, 29, 49, 26, 17, 34, 36, 28, 11, 10, 20, 45, 44, 15, 21, 0, 32, 25, 27, 37, 14, 46, 8, 38, 51, 3, 43]

This post has been edited by x68zeppelin80x: 16 May 2014 - 04:32 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1