Random Numbers in different range - C++ Data Type

C++ examples for Data Type:Random

Description

Random Numbers in different range

Demo Code

#include <cstdlib>
#include <ctime>
#include <iostream>

int main(int argc, const char *argv[]) {
    srand(time(NULL));/*from w  w  w .ja  v a2s . c  o m*/

    std::cout << "1 <= n <= 2: " << 1 + rand() % 2
              << "\n1 <= n <= 100: " << 1 + rand() % 100
              << "\n0 <= n <= 9: " << rand() % 10
              << "\n1000 <= n <= 1112: " << rand() % (1112 - 1000) + 1000
              << "\n-1 <= n <= 1: " << rand() % 3 + -1
              << "\n-3 <= n <= 11: " << rand() % 15 + -3 << std::endl;
    return 0;
}

Result


Related Tutorials