Answers for "cpp rand"

C++
7

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';
}
Posted by: Guest on May-16-2020
7

cpp rand

std::srand(std::time(nullptr)); 		// set rand seed
v1 = std::rand() % 100;         // v1 in the range 0 to 99
v2 = std::rand() % 100 + 1;     // v2 in the range 1 to 100
v3 = std::rand() % 30 + 1985;   // v3 in the range 1985-2014
Posted by: Guest on January-17-2021
2

rand() c++

#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';
 
    // roll 6-sided dice 20 times
    for (int n=0; n != 20; ++n) {
        int x = 7;
        while(x > 6) 
            x = 1 + std::rand()/((RAND_MAX + 1u)/6);  // Note: 1+rand()%6 is biased
        std::cout << x << ' ';
    }
}
Posted by: Guest on May-21-2021
3

rand() c++

v1 = rand() % 100;         // v1 in the range 0 to 99  --Credit goes to Clever cowfish
v2 = rand() % 100 + 1;     // v2 in the range 1 to 100
v3 = rand() % 30 + 1985;   // v3 in the range 1985-2014
Posted by: Guest on January-24-2021

Browse Popular Code Answers by Language