help with rand()

Started by
16 comments, last by RaistlinMajere 20 years, 7 months ago
Heads up,

VbDestroyer is correct - If you use the method stated, you will get duplicate cards (well... you have a > 99% chance getting at least one duplicate)

To do it better (one right way of doing it) :

First set up the cards in your array so that each card (if it is indexed 0 to 51) has it's ideal index value.

Then loop though each card. For each card select a "random" value from 0 to 51 ( rand() % 52; )
That's the second card's index. Swap the card's places.
ie.
temp=card; card=card[j]; card[j]=card;  </pre>  <br>It's ok if i=j by chance.<br><br>You can loop though multiple times if you aren't satisfied by the results, but hey - at least now you won't have duplicates.<br><br>Good Luck,<br>-Michael<br><br>edit: stupid thing thinks the  is italics<br>Btw, this is kinda like the oposite of what's known as the Selection Sort: Check it out.  </i>  <br><br><SPAN CLASS=editedby>[edited by - thr33d on September 2, 2003 7:50:40 PM]</SPAN>
Advertisement
thanks guys that helped
-----------------------------------------------------------"Shut up and eat your cheese sandwhich"
quote:Original post by Thr33d
Then loop though each card. For each card select a "random" value from 0 to 51 ( rand() % 52; )
That''s the second card''s index. Swap the card''s places.
ie.
temp=card[‍i][‍i]; card[‍i]=card[j]; card[j]=card[‍i];   

It''s ok if i=j by chance.


std::random_shuffle(card, card+52);


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
black jack is played with 4 decks, just so you know. That's 208 cards.

Do you use your powers for good or for awesome?
|GYAH!!! | Association of Computing Machinery|

[edited by - capn_midnight on September 2, 2003 10:38:26 PM]

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

quote:Original post by Thr33d
Then loop though each card. For each card select a "random" value from 0 to 51 ( rand() % 52; )
That''s the second card''s index. Swap the card''s places.
Not completely random.
You choose 52 random numbers from [0, 52), so there are 5252 equally likely sequences of numbers outputted by the RNG. However, there are only 52! permutations of a deck of cards. Since 5252 is not evenly divisible by 52!, some permutations must be more likely than others.

Instead, loop through each card (except the first). For each card i, swap it with a random card j where 0 <= j <= i.
In other words: std::random_shuffle(card, card+52).
Beer Hunter:

Yeah, you're right (so far as I can tell) I'm not so great with proofs of stuff in math, I kinda work from my mind and the practical side of things.

But what you say makes sense:
In the way I mentioned, you repick many of the first cards to be switched with the later cards. I don't really see how this could somehow create any less random distrubution of sets of cards, but, ah, I can see that my concept was off base.

You're right also with the 52^52 and the 52! math parts I now see.

When going through a deck of cards, the first card is (theoretically) completely random. As you choose the next card, it is a random card of the remaining. (My logic is correct, right?) When you get to the last card you need not swap it, it has most likely been swapped, and if not, well, it's just part of the chance of things (required for even spread of cards.)

So really it should look something like

int j;
for(int ì = 0; ì < 51; ì++)
{
j = rand() %(52-ì)+ì;
swap(cards[ì], cards[j]);
}

the swap function would be defined somewhere else (just swaps two variable's values)

As I saw it though, in what you wrote
"
Instead, loop through each card (except the first). For each card i, swap it with a random card j where 0 <= j <= i.
"

You seem to be taking a backwards approach to it, but it doesn't quite look right. (Because you're swapping swapped cards, which seems really funky to me - kinda like what I was doing before)

"
In other words: std::random_shuffle(card, card+52).
"

Can't comment on this - I don't know how random_shuffle works
I'm more of a Java man myself.

Please tell me if my math is off, and explain to me if I'm wrong in my interpretation of your listed method of shuffling. Also, please let me know how random_shuffle works if you reply to other stuff of mine.

Thanks,
-Michael

[edited by - thr33d on September 3, 2003 11:45:24 AM]
template<typename _RandomAccessIter>  inline void  random_shuffle(_RandomAccessIter __first, _RandomAccessIter __last)  {    // concept requirements    __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<          _RandomAccessIter>)    if (__first == __last) return;    for (_RandomAccessIter __i = __first + 1; __i != __last; ++__i)      iter_swap(__i, __first + __random_number((__i - __first) + 1));  }


For all elements i in [first;last) swap i with a random element in (i;last).

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by capn_midnight
black jack is played with 4 decks, just so you know. That''s 208 cards.

Do you use your powers for good or for awesome?
|GYAH!!! | Association of Computing Machinery|

[edited by - capn_midnight on September 2, 2003 10:38:26 PM]


some casinos use 6 decks also, depends on house rules...

This topic is closed to new replies.

Advertisement