You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
1.0 KiB
23 lines
1.0 KiB
#include <iostream> // Required for input/output operations (e.g., std::cout)
|
|
#include <cstdlib> // Required for rand() and srand() functions
|
|
#include <ctime> // Required for time() function to seed the random number generator
|
|
|
|
int main() {
|
|
// Seed the random number generator using the current time.
|
|
// This ensures a different sequence of random numbers each time the program runs.
|
|
srand(static_cast<unsigned int>(time(0)));
|
|
|
|
// Generate a random number.
|
|
// rand() returns a pseudo-random integer in the range [0, RAND_MAX].
|
|
int randomNumber = rand();
|
|
|
|
// Generate a random number within a specific range (e.g., 0 to 99).
|
|
// The modulo operator (%) is used to limit the range.
|
|
int randomNumberInRange = rand() % 100;
|
|
|
|
// Print the generated random numbers.
|
|
std::cout << "A raw random number: " << randomNumber << std::endl;
|
|
std::cout << "A random number between 0 and 99: " << randomNumberInRange << std::endl;
|
|
|
|
return 0; // Indicate successful program execution
|
|
}
|