![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Posts: 12
Rep Power: 0
![]() |
Need help with random variable generation
Hey guys!
I'm programming a cards game using C. All the cards shown on the game table are organized in one single array (stack pile). The elements in the array have two atributes: int value and int suits. What I want to do is shuffle the deck in the begining of the game, distributing the cards randomly on the array. This would be done by choosing by lot positions on the array for each card in the game. Problem is, once I randomly choose a position for a card on the array, how can I make sure this position won't be randomly chosen again for another card? Is there a way through wich I can, for an istance, generate a number between 1 and 100 but excluding the number 50? This is the code I use to generate a number between 1 and 159 (my array size is 160): srand((unsigned int)time((time_t *)NULL)); randomnumber = (rand()%159)+1; P.S. lease don't notice the fact that I can't get the number 0 to be randomly generated using this code. This will be changed soon ![]() |
|
|
|
|
|
#2 |
|
Professional Programmer
|
Edited: misread post
![]() |
|
|
|
|
|
#3 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
would it work to just iterate through the array, swapping the current card with a random one? or iterate through 1/2, swapping with cards from the other 1/2? (or anything to that effect). Even if a card gets swapped twice, it will still be in a somewhat random position...
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
Here's an idea (similar to Jimbo's): Say you start with 160 cards in random order. You choose the first card, then swap it with the last card. You choose the next card from the 159 cards remaining, then swap it with the second to last card. At the end of the day, the chosen cards have been selected and moved to the back of the deck.
If the deck has been properly shuffled, this should be unneccessary, because each card taken off the top of the deck should be random. |
|
|
|
|
|
#5 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#6 |
|
Expert Programmer
|
No, you would only do this for the few (say five) cards you were selecting. Then reshuffle.
I can see how this would be a problem if you intended to only shuffle the deck once. But hey, it's just an idea. |
|
|
|
|
|
#7 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Quote:
![]() |
|
|
|
|
|
|
#8 |
|
Expert Programmer
|
Ohhh... I misunderstood. I though he was talking about selecting cards randomly from an already shuffled deck. Actually shuffling the deck is another story. Whoops!
If all you're trying to do is shuffle the deck, why not randomly swap cards with each other throughout the deck? If, for some reason, you want to move each card only once (to make sure they were all shuffled?), you could do the same thing I described; choose a random card and move it to the back of the deck. Repeat with remaining cards. I suppose that's sort of what you (Jimbo) said to begin with. ![]() |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Feb 2006
Posts: 12
Rep Power: 0
![]() |
Thanks for the replies guys!
It seem Titanium's idea could work... I could just start the array with a know position of cards, and then change their positions on a random way. I'll try that and let you know if I am suscessful. Thanks again ppl! |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Mar 2006
Location: Olympia WA USA
Posts: 11
Rep Power: 0
![]() |
Rather than swapping ...
How about this?
Generate the array of cards as you are now. To pick a card, generate a random number using the mod operation you stated, copy the value and suit to working variables, and set the array values in that position to -1. This presumes, of course, that you set up your selection logic to first generate a position in the array, then check it for a -1 and, if it finds that value, loop to generate another position. Alternatively, move to the next position in the array and check that for a -1 ... doing this until you get to the end of the array or until you find a usable value. I'd be happy to work out a little demo if that would help. Tom |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|