random number generator c++ using srand 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: how to make a random number in c++

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main() {
	srand(time(NULL)	);
	const char arrayNum[7] = {'0', '1', '2', '3', '4', '5', '6'};
	int RandIndex = rand() % 7;
	cout<<RandIndex<<endl;
	return 0;
}

Tags:

Cpp Example