Algoteka
Log in to submit your own samples!

Generating a Random Integer

Problem by Andres_Unt
# Tech tags Title Creator Created date
1 1
2022-10-08 15:00
View all samples for this language (1 verified and 0 unverified)

The simplest way | C++ |

By Andres_Unt |
1 likes

Code

#include<iostream>
#include<ctime>
int main() {
    //Seed the rng to avoid getting the same number every time.
    //This only changes once per second. Use more precise clock if necessary.
    srand(time(0));

    std::cout << rand() << '\n';
    return 0;
}

References

objects
std::cout en.cppreference.com cplusplus.com
functions
rand en.cppreference.com cplusplus.com
srand en.cppreference.com cplusplus.com
std::basic_ostream::operator<< en.cppreference.com cplusplus.com
time en.cppreference.com cplusplus.com

Problem Description

Generate a random integer using a random number generator.

Possible characteristics to optimize for are simplicity, quality of the RNG algorithm, and speed of the RNG algorithm.

View sample discussion (0 comments)
View problem discussion (0 comments)