Rand Creates the Same Random Number Over and Over Again

rand() always gives same number

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

Anyway, every fourth dimension i use rand() i get the same numbers every single time I run the program. IE, the first "hand" that is played is always [ vii, eleven ]. How tin can I make information technology so that the generated numbers are random every time?

This lawmaking is not complete for a blackjack program, but I demand to overcome this rand() issue before I motion on to other calculations. It currently compiles and works as is without whatever other issues.

Thank you so much in advance.

                          one
two
iii
iv
five
6
7
viii
9
10
11
12
13
xiv
xv
xvi
17
18
19
xx
21
22
23
24
25
26
27
28
29
thirty
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 <<                            "Endeavor to go as close to 21 without going over."                            << endl;         cout <<                            "HIT ways get another card (value), STAY means keep current values and."                            << endl;         cout <<                            "Permit's begin!"                            << endl << endl;                            while                            (loop1 == 0)         {                            int                            usr_card1 = rand() % 11 + 1;                            int                            usr_card2 = rand() % 11 + one;                            int                            deal_card1 = rand() % 11 + 1;                            int                            deal_card2 = rand() % eleven + i;                            int                            usr_total = usr_card1 + usr_card2;                            int                            deal_total = deal_card1 + deal_card2;                  cout <<                            "You accept: [ "                            << 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 ==                            'south'                            || ans ==                            'Southward')                            pause;                            else                            return                            0;                 }                 cout << usr_total << endl << endl;                 cout <<                            "Play once again? (Y/Due north): ";                 cin >> ans2;                            if                            (ans2 ==                            'y'                            || ans2 ==                            'Y')                            go along;                            else                            if                            (ans2 ==                            'n'                            || ans2 ==                            'N')                            break;         }                            render                            0; }                        

You oasis't seeded the C random number generator by calling Once srand() ;

Add the following line before you enter your while loop:

srand(time(Zilch));

Y'all will likewise need to include the <ctime> header.

I would propose y'all 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 improve ways to generate random numbers available.

Last edited on

You lot need to seed it.

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

For example.

                          1
2
iii
4
5
half dozen
7
8
9
x
xi
12
thirteen
xiv
15
16
17
18
19
20
                                                      // This program demonstrates random numbers.                            #include <iostream>                            #include <cstdlib>                                                        // rand and srand                            #include <ctime>                                                        // For the fourth dimension role                            using                            namespace                            std;                            int                            main() {                            // Get the system fourth dimension.                            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 instance of the numerous methods to obtain random value.

                          1
2
3
4
five
vi
7
viii
9
10
11
12
thirteen
xiv
15
16
17
18
19
xx
21
22
23
24
25
26
27
28
29
thirty
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 betwixt 1.0 and 10.0:\north";                            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 < ten; ++i)     { 	   std::cout << distributionInteger(generator) <<                            " ";     }      std::cout << std::endl;                            render                            0; }                        

Last edited on

Thanks then much! These were both very helpful!

Topic archived. No new replies allowed.

thomasclaill76.blogspot.com

Source: http://www.cplusplus.com/forum/beginner/230838/

0 Response to "Rand Creates the Same Random Number Over and Over Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel