I am trying to take a number and give it a 50/50 chance of adding .5 or subtracting .5 without going negative or over 22. This is what I got but I am completely new and know its wrong but I can't find how to do it in the articles.
usingnamespace std;
int main ()
{
srand(time(0));
int your_number;
cout <<"Please enter a number" << endl;//prompt
cin >> your_number; //input
for (int i = 1; i <= 22; i +- 0.5)
{
double r = rand()*22 / RAND_MAX;//between 0 and 22
cout << r << endl;
}
system("pause");
return 0;
}
You don't need the for loop and for the random, make it so it randomly generates either a 1 or 0, then check the number and make it add if it a 1 or take away if it a 0.