Imagine: You get a Calculator from somebody, and you are making some equations on it. However, the Calculator seems to be broken, since it doesn't gives back the correct answer. This is a very funny program to use on somebody you lend your calculator to.
Version 2: Using Random Numbers Edit
The program above always gives a number back that is one higher than the answer that would be correct. However, it is kind of easy to tell what is wrong with this calculator. Therefore, a better idea would be:
Now the answer is randomly two less, one less, one more, two more and sometimes the correct anwer. Of course you can change the -2 and the 2 for any value you want.
Version 3: procedurally Edit
However, when the user enters the same calculation twice, they will get a different answer. This kind of gives the trick away. An better idea would be to not randomly generate the answer, but procedurally generate(a random looking value, but every time the same one for the same input) the answer. What is this? You will see...
Ouch! That looks kind of difficult now. Don't worry, in fact it is quite easy to understand.
The new part does this: The Root of A is taken, and only the part after that (that is what fPart does) is saved into C.
Afterwards, C is multiplied by 10, and then 4,5 is substracted. Why? This again creates a random-looking part. It creates some number between -4,5 and 4,5.
If you don't get it, here's an example:
- √(5) is 2.236067977.
- The fPart of that is 0.236067977.
- Multiplied by ten, this is 2.36067977,
- Minus 4.5 is -2.1394
- Taken the iPart of this, the number is -2.
Version 4: Negative Roots Fix Edit
The program still has one drawback: you can't use square roots on negative values. However, abs( will return the same positive number for any input, so can be used to ensure square roots always work.
Of course, you can upgrade this program even further: by assigning a random value to a variable before the while loop, you can for that run of the program create unique numbers. Also, it is possible to end the program silently when entering a hidden password number or something. But that is something you can find out yourself.