0

I'm creating a blackjack card counting simulator in c++ and I'm starting off by making the program choose from the number of cards avaviable. I have assinged each card with a starting value of 4(execpt for 10 and clothed cards as they all have the same value) as there's 4 of each number in a complete deck. However I want these numbers to go down as a card is drawn. For example if I draw a 3 I want the value of three's to go from 4 to 3. Thank you. Code:

#include<iostream>
#include<time.h>
int acecard = 4;
int twocard = 4;
int threecard = 4;
int fourcard = 4;
int fivecard = 4;
int sixcard = 4;
int sevencard = 4;
int eightcard = 4;
int ninecard = 4;
int tencard = 16;
//shows how many of each card there is


int cards[10] = { acecard, twocard, threecard, fourcard, fivecard, sixcard, sevencard, eightcard, ninecard, tencard };
//stores all the card in an array
int cardpick()
{
    srand(time(NULL));
    int Ranindex = rand() % 10;
    return cards[Ranindex];//picks random card from array
}


int main()
{
    return cardpick();
}

I tried to use if functions via something like this:

    srand(time(NULL));
    int Ranindex = rand() % 10;
    int update = cards[Ranindex];//picks random card from array
    if (update = acecard)
    {
        acecard -= 1;
    }

However since most cards have the same starting value it would make the value go down for the card no matter what card was drawn. Thank you.

  • Immediately, `if (update = acecard) { ... }` doesn't do what you're probably expecting. You're assigning `acecard` to `update`. Unless `acecard` is `0`, this will always be true. – Chris Dec 07 '22 at 15:22
  • Side note: Don't use `NULL` in modern C++, use [nullptr](https://en.cppreference.com/w/cpp/language/nullptr). Modern C++ has [much better random number facilities](https://en.cppreference.com/w/cpp/numeric/random) than the crufty `srand`/`rand` it inherited from C. Prefer [std::array](https://en.cppreference.com/w/cpp/container/array) or [std::vector](https://en.cppreference.com/w/cpp/container/vector) over C-style arrays (just forget that those exist). `rand() % 10;` introduces bias in your numbers - see "pigeon hole principle" - `std::uniform_int_distribution` avoids such bias. – Jesper Juhl Dec 07 '22 at 15:24
  • Given nothing from `` has been used, it seems like this could just as easily be tagged for C rather than C++. – Chris Dec 07 '22 at 15:37
  • Ohh and `time(NULL);` is a rather horrible seed for a pseudo random number generator. Two users running the code within the same second will get the same number sequence. Better mix in some more unique stuff in the seed. Suggestions: The process ID, the CPU ID, the MAC address of their NIC, the timezone, the current nanoseconds of their clock, their username, entropy from the kernel, the address of `main` in memory, the time since last reboot, etc. Preferably multiple such things, mixed together. – Jesper Juhl Dec 07 '22 at 15:41

0 Answers0