# Trying to assign random numbers to a deck of cards

Page 1 of 1

## 3 Replies - 534 Views - Last Post: 16 May 2014 - 04:07 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=347015&amp;s=c77450ac6738f8b5addbd3e8de0d9f79&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 armonsafai

Reputation: 0
• 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

• A little too OCD

Reputation: 417
• 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();
}
}
```

### #3 g00se

• D.I.C Lover

Reputation: 3032
• Posts: 12,955
• 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];
```

### #4 x68zeppelin80x

Reputation: 130
• 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