Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

write a c++ programm printing 3 random number using defaultrandomengine

  1. User

    write a c++ programm printing 3 random number using default_random_engine

  2. Assistant

    Sure! Here's a C++ program that uses default_random_engine to print 3 random numbers:

    #include <iostream>
    #include <random>
    
    int main() {
        // Create a random number generator using default_random_engine
        std::default_random_engine eng(std::random_device{}());
    
        // Create a uniform distribution that generates random numbers between 1 and 100 (inclusive)
        std::uniform_int_distribution<int> dist(1, 100);
    
        // Print 3 random numbers
        for (int i = 0; i < 3; ++i) {
            int num = dist(eng);
            std::cout << num << " ";
        }
        std::cout << std::endl;
    
        return 0;
    }
    

    In this program, we create a random number generator eng using default_random_engine and seed it with a random device. Then, we create a uniform distribution dist that generates random integers between 1 and 100 (inclusive).

    Inside the loop, we generate a random number using dist(eng) and print it. Finally, we output a newline character after printing the 3 numbers.

    Note that the random_device class is used to obtain non-deterministic random numbers for seeding the generator.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.