srand outside loop

 avatar
unknown
c_cpp
a year ago
442 B
4
Indexable
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
    int num;

    // Seed the random number generator outside the loop to get new number each time
    
            srand(time(NULL));

    for (int i = 0; i < 100; i++) {
        // if you put the seed there on each iteration it will likely give the same number
        
        num = rand() % 7 + 3;
       cout << num << " ";
    }

    return 0;
}
Editor is loading...
Leave a Comment