My understanding is that this produces a random number between 10 and offset:
random = (rand() % 10) + offset;
offset gets incremented by 1 but never goes above 10, yet when I run this code the variable random gets set to numbers > 10.
code in question:
#include "pch.h"
#include <cstdlib>
#include <Windows.h>
#include <iostream>
using namespace std;
void gen(int offset)
{
int random;
if (offset != 10)
{
random = (rand() % 10) + offset;
cout << "random should be between: " << 10 << " and " << offset << endl;
cout << "random: " << random << endl << endl;
Sleep(500);
gen(++offset);
}
}
int main()
{
srand(373);
gen(1);
cin.get();
}
and the output:
random should be between: 10 and 1
random: 7
random should be between: 10 and 2
random: 11
random should be between: 10 and 3
random: 3
random should be between: 10 and 4
random: 13
random should be between: 10 and 5
random: 10
random should be between: 10 and 6
random: 13
random should be between: 10 and 7
random: 16
random should be between: 10 and 8
random: 14
random should be between: 10 and 9
random: 18
Aucun commentaire:
Enregistrer un commentaire