rand() always gives same number

I just started learning c++ and I was trying to recreate an old blackjack program I had made in high school to test what I have learned in my course.

Anyway, every time i use rand() i get the same numbers every single time I run the program. IE, the first "hand" that is played is always [ 7, 11 ]. How can I make it so that the generated numbers are random every time?

This code is not complete for a blackjack program, but I need to overcome this rand() issue before I move on to other calculations. It currently compiles and works as is without any other issues.

Thank you so much in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
        int loop1 = 0;
        int loop2 = 0;
        char ans;
        char ans2;

        cout << endl;
        cout << "Welcome to black jack!" << endl;
        cout << "Try to get as close to 21 without going over." << endl;
        cout << "HIT means get another card (value), STAY means keep current values and." << endl;
        cout << "Let's begin!" << endl << endl;

        while (loop1 == 0)
        {
                int usr_card1 = rand() % 11 + 1;
                int usr_card2 = rand() % 11 + 1;
                int deal_card1 = rand() % 11 + 1;
                int deal_card2 = rand() % 11 + 1;
                int usr_total = usr_card1 + usr_card2;
                int deal_total = deal_card1 + deal_card2;

                cout << "You have: [ " << usr_card1 << ", " << usr_card2 << " ]" << endl;

                while (loop2 == 0)
                {
                        cout << "Hit or Stay? (H/S): ";
                        cin >> ans;
                        if (ans == 'h' || ans == 'H')
                        {
                                int usr_cardn = rand() % 11 + 1;
                                usr_total = usr_cardn + usr_total;
                                cout << "[ " << usr_cardn << " ]" << endl;
                                continue;
                        }
                        else if (ans == 's' || ans == 'S')
                                break;
                        else
                                return 0;
                }
                cout << usr_total << endl << endl;
                cout << "Play again? (Y/N): ";
                cin >> ans2;
                if (ans2 == 'y' || ans2 == 'Y')
                        continue;
                else if (ans2 == 'n' || ans2 == 'N')
                        break;
        }
        return 0;
}
closed account (E0p9LyTq)
You haven't seeded the C random number generator by calling ONCE srand();

Add the following line before you enter your while loop:

srand(time(NULL));

You will also need to include the <ctime> header.

I would suggest you get away from using the C pseudo random number generator and look into the C++ library <random>.

The C standard recommends not using rand/srand if there are better ways to generate random numbers available.
Last edited on
You need to seed it.

1
2
  /* initialize random seed: */
  srand (time(0));


For example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// This program demonstrates random numbers.
#include <iostream>
#include <cstdlib>   // rand and srand
#include <ctime>     // For the time function
using namespace std;

int main()
{
   // Get the system time.
   unsigned seed = time(0);
   
   // Seed the random number generator.
   srand(seed);
   
   // Display three random numbers.
   cout << rand() << endl;
   cout << rand() << endl;
   cout << rand() << endl;
   return 0;
}


This is just an example of the numerous methods to obtain random value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <chrono> //For system_clock
#include <random>

int main()
{
    // construct a trivial random generator engine from a time-based seed:
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    std::default_random_engine generator(seed);


    std::uniform_real_distribution<double> distributionDouble(1.0, 10.0); // Set the numbers for double.
    std::uniform_int_distribution<int> distributionInteger(1, 10); // Set the numbers for int.

    std::cout << "some random numbers between 1.0 and 10.0:\n";
    for (int i = 0; i < 10; ++i)
    {
	   std::cout << distributionDouble(generator) << " ";
    }
    std::cout << std::endl;

    std::cout << "some random numbers between 1 and 10: ";
    for (int i = 0; i < 10; ++i)
    {
	   std::cout << distributionInteger(generator) << " ";
    }

    std::cout << std::endl;

    return 0;
}
Last edited on
Thanks so much! These were both very helpful!
Topic archived. No new replies allowed.