rand function Cpp code example
Example 1: cpp rand
std::srand(std::time(nullptr));
v1 = std::rand() % 100;
v2 = std::rand() % 100 + 1;
v3 = std::rand() % 30 + 1985;
Example 2: cpp random int
v1 = rand() % 100;
v2 = rand() % 100 + 1;
v3 = rand() % 30 + 1985;
Example 3: rand() c++
v1 = rand() % 100;
v2 = rand() % 100 + 1;
v3 = rand() % 30 + 1985;
Example 4: rand() c++
#include <cstdlib>
#include <iostream>
#include <ctime>
int main()
{
std::srand(std::time(nullptr));
int random_variable = std::rand();
std::cout << "Random value on [0 " << RAND_MAX << "]: "
<< random_variable << '\n';
for (int n=0; n != 20; ++n) {
int x = 7;
while(x > 6)
x = 1 + std::rand()/((RAND_MAX + 1u)/6);
std::cout << x << ' ';
}
}