Skip to main content

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.


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

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex