I'm working on a game, the screen goes from -200 to 200, and I need stuff to appear anywhere on it. It always just appears in the positive side. How do I get it to generate a negative number sometimes?
1 2 3 4 5 6 7
bool set = false;
do
{ int x = rand();
if (x > -190 && x < 190)
{ point.setX(x);
set = true;}
} while (set == false);
jonnin, did you mean to put parentheses around the (rand() % 200) part?
If so, that produces 0 as a result twice the amount of time as everything else.
pow(-1, rand()) --> set{-1, 1}
(rand() % 200) --> range[0, 199]
-1 * 0 == 0
1 * 0 == 0
Calling rand() more times then necessary will probably just make things less random. Agreed, using uniform_int_distribution would easy here.
I can't even look at it now, really bad headache today.
its supposed to be -1 to a random power (gonna give 1 or -1) times a value.
rand has so many problems, I don't know that it matters, though. I just assume anyone using rand is not worried about lack of randomness or issues. Calling it extra times cuts the period down so it repeats sooner, is the only effect I know of.
you will get 2x zeros as any number, yes. the reason is that for any other number, the count is split in half roughly, with 1/2 being positive, and 1/2 being negative, and all the zeros are lumped together. Its not making twice as many zeros as before, but if you did a count of all values zero would show up twice as much. If you did a count of abs of all values, it would be about equal. Good find.