How to subtract 0.5 from this random
Sep 24, 2012 at 4:21pm UTC
How do I make it randomize a -0.5 in addition to the 0.5? In other words how do I make it randomize so the output is sometimes 1.5, 2.0, 2.5, 1.5, 2.0 when I enter 2 into the program? I can only get it to add 0.5. Hope that makes sense
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
double r;
int user_in;
cout <<"enter number" << endl;
cin >> user_in;
srand(time(0));
for (int i=0; i<=11; i++)
{
r=rand() %2 * 0.5 + user_in;
cout << r << endl;
}
system("pause" );
return 0;
}
Sep 24, 2012 at 4:36pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
double r;
int user_in;
cout <<"enter number" << endl;
cin >> user_in;
srand(time(0));
for (int i=0; i<=11; i++)
{
r= (rand() % 2 ? 0.5 : 0.0) + rand() % user_in;
cout << r << endl;
}
return 0;
}
Sep 24, 2012 at 4:46pm UTC
you're the man
Topic archived. No new replies allowed.