Why does this line break the whole system I made? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why does this line break the whole system I made?

In this code bit line 31 completely breaks the whole chance-damage system. It multiplies it completely out of proportion from a line of code that seems to have absolutely nothing to do with the calculation. I have no idea if I'm doing something wrong or what, because it works just fine if I move it somewhere else. I'm just curious as to what is causing this problem. The maximum resulting damage number should be 1000, but it goes into the 10s of millions. https://code.sololearn.com/ch8uh43kxNG4/?ref=app

6th Nov 2021, 12:48 AM
Caleb Plays Minecraft Now
Caleb Plays Minecraft Now - avatar
3 Answers
+ 5
As Ipang says, you only seed random once. Remove line 20. A big part of your issue is line 15. You never initialized a value for CM, so it will start with a garbage value depending on what was left in memory at the location for that variable. Try setting it to 1 or another value that makes sense for your program/algorithm.
6th Nov 2021, 1:11 AM
ChaoticDawg
ChaoticDawg - avatar
+ 6
Not understanding your code flow. Just wanna ask why you call srand() twice inside damage()? at line 17 and 20. Try to call srand() once in the main() instead.
6th Nov 2021, 1:05 AM
Ipang
+ 6
Thanks for the input, that helped a lot! I removed the extra srand() and set the default value of CM, and now it works just fine!
6th Nov 2021, 1:15 AM
Caleb Plays Minecraft Now
Caleb Plays Minecraft Now - avatar