gen random number c++ code example

Example 1: c++ generate random numbers

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	srand(time(0));

	for (int i = 0; i <= 10; i++)
	{
		cout << rand() % 10 << " ";
	}
}

Example 2: c++ random

#include <cstdlib>
#include <iostream>
#include <ctime>
 
int main() 
{
    std::srand(std::time(nullptr)); // use current time as seed for random generator
    int random_variable = std::rand();
    std::cout << "Random value on [0 " << RAND_MAX << "]: " 
              << random_variable << '\n';
}

Tags:

Php Example