[Solved] How can I execute random results in c++ as the code given below? [closed]


One easy way is to seed the basic random number generator with the current time then use rand() reduced modulo the number of possible results to choose a random element from results

#include "iostream"
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
    int results[] = {1, 2, 3, 4, 5};
    srand(time(NULL));
    cout << results[rand()%(sizeof(results)/sizeof(results[0]))] << endl;
    return 0;
}

6

solved How can I execute random results in c++ as the code given below? [closed]