C

Asked • 03/26/19

Why does rand() + rand() produce negative numbers?

I observed that `rand()` library function when it is called just once within a loop, it almost always produces positive numbers. for (i = 0; i < 100; i++) { printf("%d\\n", rand()); }But when I add two `rand()` calls, the numbers generated now have more negative numbers. for (i = 0; i < 100; i++) { printf("%d = %d\\n", rand(), (rand() + rand())); }Can someone explain why I am seeing negative numbers in the second case?PS: I initialize the seed before the loop as `srand(time(NULL))`.

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.