See the top rated post in this thread. Click here

Results 1 to 13 of 13

Thread: Random Shuffling Code Snippet

  1. #1


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Random Shuffling Code Snippet

    The following is a snippet of the random shuffle I use in my BJ programs. I user the Mersenne Twister but obviously you can use any RNG with it.

    Code:
    void shuffle(std::vector<short> & shoe)
    {
    	short N = shoe.size();  // number of cards in shoe
    	short i, j, max, temp;
    	for (i = N; i > 1; --i)
    	{
    		max = i / 2;
    		j = floor(max + myRNG(max)); // random index j
    		temp = shoe[j];
    		shoe[j] = shoe[i - 1];
    		shoe[i - 1] = temp;
    	}
    }
    Chance favors the prepared mind

  2. #2
    Junior Member
    Join Date
    Dec 2011
    Location
    Rosario (Argentina)
    Posts
    27


    Did you find this post helpful? Yes | No
    Thank you!

  3. #3


    Did you find this post helpful? Yes | No
    The shuffle I use is my adaptation of the Knuth Shuffle.

    specificShoe[] is the shoe array with cards numbered 0 to (52*decks-1). I use short integers as I never planned to sim more than 8 decks.

    totCards is number of cards in a full shoe = 52*decks.

    unsigned long getRandNum(const unsigned long &max) is a function returning a random number (in the range 1 to max) output by a random number generator of your choice.
    Code:
    void shuffle(short specificShoe[], const long &totCards) {
     unsigned long i = (unsigned long)totCards;
     while (i > 1) {
      unsigned long j = getRandNum(i);
      short temp = specificShoe[i - 1];
      specificShoe[i - 1] = specificShoe[j];
      specificShoe[j] = temp;
      i--;
     }
    }
    k_c

  4. #4


    Did you find this post helpful? Yes | No
    So basically you're going from card #52 to card #1. You're choosing a random card and swapping the two? Also, do you rearrange the deck before shuffling, or do you take it out of the discard rack and shuffle that? This is code you've written yourselves?
    "Everyone wants to be rich, but nobody wants to work for it." -Ryan Howard [The Office]

  5. #5


    Did you find this post helpful? Yes | No
    Quote Originally Posted by RollingStoned View Post
    So basically you're going from card #52 to card #1. You're choosing a random card and swapping the two? Also, do you rearrange the deck before shuffling, or do you take it out of the discard rack and shuffle that? This is code you've written yourselves?
    1. Start with a full shoe (totCards=52*decks)
    2. Array elements are numbered 0 to (52*decks-1)
    3. Swap last element with randomly selected element in the range 0 to totCards-1 - note last element may not change because it may be swapped with itself
    4. Last element is now filled and eliminated
    5. Decrement totCards by 1 and repeat from remaining elements until 1 card remains - note element 0 may not have changed if none of the previous swaps didn't randomly choose element 0; chance of element 0 not ever being selected = 1/2*2/3*3/4*.....*(totCards-1)/52*decks = 1/(52*decks)

    This algorithm made sense to me because you initially randomly get 1 card out of all of the cards in a full shoe, then 1 card out of successively fewer and fewer cards, which is what would happen if each card was successively randomly drawn from a full shoe.

    Re your question: It doesn't matter the initial order of the cards. Each successive shuffle reorders them.

    k_c

  6. #6


    Did you find this post helpful? Yes | No
    Why don't you just use std::random_shuffle?

  7. #7


    Did you find this post helpful? Yes | No
    Because he isn't using C++

    std::random_shuffle is basically what's being implemented in with the above code. It works in pretty much the same way. So I'd rather implement it than use the library code so that I can use it in my C, C++, and Obj-C codebases.

  8. #8
    Random number herder Norm's Avatar
    Join Date
    Dec 2011
    Location
    The mote in God's eye
    Posts
    12,476
    Blog Entries
    59


    Did you find this post helpful? Yes | No
    If random shuffle uses a compiler supplied RNG, it's useless. Which I imagine is why ICNT called myRNG. That is, his own.
    "I don't think outside the box; I think of what I can do with the box." - Henri Matisse

  9. #9


    Did you find this post helpful? Yes | No
    You can pass your own RNG to random_shuffle as an optional third parameter.

  10. #10


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No
    i need help i have $10,000 Bankroll 6deck S17 minimum bet $25 how much positive T/C and how much maximum bet can i bet.thanks

  11. #11
    Senior Member
    Join Date
    Dec 2011
    Location
    3rd rock from Sol, Milky Way Galaxy
    Posts
    14,158


    Did you find this post helpful? Yes | No
    Not enough info. Go to Bj resources in the tool bar along the top of the screen. Click on CVCX online in the next tool bar. Input all the appropriate info and you have a ramp, its RoR and the hourly, SD, N0 etc.

  12. #12


    Did you find this post helpful? Yes | No
    norm,what the difference CVBJ and CVCX ?

  13. #13
    Random number herder Norm's Avatar
    Join Date
    Dec 2011
    Location
    The mote in God's eye
    Posts
    12,476
    Blog Entries
    59


    Did you find this post helpful? Yes | No
    Quote Originally Posted by rimar697 View Post
    norm,what the difference CVBJ and CVCX ?
    One id for practice and one is a high-speed simulator, calculator.
    "I don't think outside the box; I think of what I can do with the box." - Henri Matisse

Similar Threads

  1. Simulation Code in Python
    By rvince in forum Software
    Replies: 12
    Last Post: 07-04-2013, 06:45 AM
  2. Replies: 7
    Last Post: 06-12-2005, 10:32 AM
  3. Replies: 2
    Last Post: 12-27-2004, 07:14 AM
  4. Damon: BlackJack Code
    By Damon in forum Blackjack Main
    Replies: 2
    Last Post: 09-30-2001, 12:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

About Blackjack: The Forum

BJTF is an advantage player site based on the principles of comity. That is, civil and considerate behavior for the mutual benefit of all involved. The goal of advantage play is the legal extraction of funds from gaming establishments by gaining a mathematic advantage and developing the skills required to use that advantage. To maximize our success, it is important to understand that we are all on the same side. Personal conflicts simply get in the way of our goals.