Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ Random Numbers #196

Open
Qingquan-Li opened this issue Jun 22, 2022 · 0 comments
Open

C++ Random Numbers #196

Qingquan-Li opened this issue Jun 22, 2022 · 0 comments
Labels

Comments

@Qingquan-Li
Copy link
Owner

1. rand()

The C++ library has a function, rand(), that you can use to generate random numbers.
The rand() function requires the <cstdlib> header file.

The number returned from the function is an int. Here is an example of its usage:

y = rand(); // 16807 (on macOS)

pseudorandom:

In actuality, the numbers produced by rand() are pseudorandom.
The function uses an algorithm that produces the same sequence of numbers each time the program is repeated on the same system.

cout << rand() << endl;
cout << rand() << endl;
cout << rand() << endl;
// Output the same numbers each time (on macOS):
// 16807
// 282475249
// 1622650073

The three numbers displayed will appear to be random, but each time the program runs, the same three values will be generated.

2. srand()

In order to randomize the results of rand(), the srand() function must be used.
srand() accepts an unsigned int argument, which acts as a seed value for the algorithm.
By specifying different seed values, rand () will generate different sequences of random numbers.

A common practice for getting unique seed values is to call the time function, which is part of the standard Iibrary.
The time function returns the number of seconds that have elapsed since midnight, January 1, 1970.
The time function requires the <ctime> header file, and you pass 0 as an argument to the function.

// This program demonstrates
// generating three different random numbers each time.
#include <iostream>
#include <cstdlib> // For 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;

    cout << endl << "The second time: " << endl;
    seed++;
    srand(seed);
    cout << rand() << endl;
    cout << rand() << endl;
    cout << rand() << endl;

    return 0;
}
1903986180
641903313
1658622710

The second time: 
1904002987
924378562
1133789136

3. Demonstration

/* Ask the user for the number of cards they will draw from
 * a deck of playing cards.
 * 13 numbers/faces: A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K
 *
 * We will assume that a card that's drawn will be placed back
 * into the deck so that it can be drawn again.
 *
 * Print the card's number.
 */

#include <iostream>
#include <string>
#include <cstdlib> // For rand (random) and srand (seed random) function
#include <ctime>   // For the time function

using namespace std;

// Get the system time.
unsigned seed = time(0);

int main() {
    int draw_num;
    int card_num;
    string card_num_str;

    // Seed the random number generator
    srand(seed);

    cout << "How many cards will you draw from the deck? ";
    cin >> draw_num;
    for (int i = 0; i < draw_num; i++) {
        // Generate the card's number:
        // limit the range of the random number: 1-13 (card A to card K)
        // Formula: y =(rand() % (maxValue - minValur + 1)) + minValue;
        card_num = rand() % 13 + 1;

        // if it's 1, 11, 12, or 13, print A, J, Q, K for the "number").
        switch (card_num) {
            case 1:
                card_num_str = "Ace";
                break;
            case 11:
                card_num_str = "Jack";
                break;
            case 12:
                card_num_str = "Queen";
                break;
            case 13:
                card_num_str = "King";
                break;
            default:
                card_num_str = to_string(card_num);
        }

        cout << "You drew a: " << card_num_str << endl;
    }
    return 0;
}
How many cards will you draw from the deck? 5
You drew a: 10
You drew a: Queen
You drew a: 3
You drew a: 7
You drew a: Ace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant