Tuesday, February 14, 2012

Two random number generators (card dealing)


I need help with this part of my code. This part deals with generating 2 random numbers and using that random number to display a card in each of 2 label boxes simultaneously. The problem here is the random numbers are not getting generated properly and thus cards are not displayed properly (there is repetition, sometimes no display, etc)



Basics of my code: Let us assume h, which is a variable from another part of the code, is any number between 1 and 53 (each number pertaining to a card). If the random number generated (non-repetition) matches the variable h the timer stops.



So its basically like having a deck of cards and dealing out the cards evenly to 2 people, but here the dealing stops once a number pertaining to a card (number) randomly taken is matched.



(l3,l4 are label names) Global variables:




Random rng = new Random();
List<Integer> generated = new ArrayList<Integer>();
List<Integer> generated2 = new ArrayList<Integer>();
int l3count;
int l4count;
int Ulim = 53;
int Llim = 1;
int next;
int check;
int h;
int next2;
int Ulim2 = 53;
int Llim2 = 1;



final int p = h;
int delay2 = 1000;
final Timer timer2 = new Timer();
timer2.schedule(new TimerTask(){
public void run(){

for (int i = 1; i < 53; i++)
{
while(true)
{
next = rng.nextInt(Ulim) + Llim;
if (!(generated.contains(next)||generated.contains(next2)))
{

generated.add(next);
break;
}

next2 = rng.nextInt(Ulim2) + Llim2;
if (!(generated.contains(next)||generated.contains(next2)))
{

generated.add(next2);
break;
}


}

String a = Integer.toString(next);
String c = "C:\\Users\\mycompname\\Desktop\\deck\\"+a+".png";

String d = Integer.toString(next2);
String e = "C:\\Users\\mycompname\\Desktop\\deck\\"+d+".png";

for(int j = 1;j<=53;j++)
{
if(j%2==0)
{l3.setIcon(new ImageIcon(c));
}
else
{l4.setIcon(new ImageIcon(e));
}
}


if(next==p||next2==p)
check=10;
break;
}
if(check==10)
timer2.cancel();
timer2.purge();
}

},delay2, 1000);



Any help would be appreciated. Thanks for your time.

3 comments:

  1. You'd be better off dealing the cards out randomly.

    Using the O(n) Knuth Shuffle you can pass along one card at a time, from one array to another.

    Each new array you pass the card to will be a different persons' hand.

    Keep adding 1 card at a time to each array from the deck array until each player has the number of cards your rules require.

    Also, don't rely on random numbers being equal. See stupid-sort. Look for some other pattern instead. Like a random number of cards % cards_left you should stop giving out cards.

    Update with code-sample, as per request:

    public static void shuffle (int[] array) {
    for(int n = array.length; (n > 1);) {
    int k = gen.nextInt(n--);
    int temp = array[n];
    array[n] = array[k];
    array[k] = temp;
    }
    }

    ReplyDelete
  2. The obvious problem is that Random.nextInt generates a number 0 (inclusive) and n (exclusive). So when you pass in a limit of 53, you're generating values between 0 and 52 — that's 53 possible values, and there aren't that many cards. So change the upper limit to 52.

    Also, as other answers note, the reason you're getting collisions (dealing the same card to multiple hands), etc. is that you don't want to randomly select the cards. You actually want to randomly shuffle the deck.

    Try using a shuffle algorithm instead.

    ReplyDelete
  3. If you are trying to pick a card randomly from a list you should ma a list of the cards and then pick randomly from it. The code you are using does not guarantee that you will every pick a card that you have not pick before.

    Random rng = new Random();

    // making a list of cards
    List<Integer> possibleCards = new ArrayList<Integer>();

    for ( int i=0; i < 52; i++ ){
    possibleCards.add(i);
    }

    // copy the list
    List<Integer> cardsThisRound = new ArrayList<Integer>(possibleCards);
    // randomly remove from list
    for (;cardsThisRound.size() > 0; ){
    int randomPick = cardsThisRound.remove(rng.nextInt(cardsThisRound.size()));
    test(int);
    }

    ReplyDelete