0

Why my code doesn't generate different values for x and f ? it should be random

https://code.sololearn.com/ciiJ2UZBAAyE/?ref=app

28th May 2023, 9:49 AM
Vinay Sharma
Vinay Sharma - avatar
4 Answers
+ 6
p is random, but it generates only a random value once. Shouldn't p be in the loop?
28th May 2023, 9:59 AM
Paul
Paul - avatar
+ 1
you are getting the values after 10000 iterations. Also, the range of values is not big and you are rounding. The average values of a large sample with small fluctuation will be a stable one. Try 5 loops, the value fluctuation is more apparent.
28th May 2023, 1:52 PM
Bob_Li
Bob_Li - avatar
+ 1
The reason your code is not generating different values for x and f is because you are not reassigning the values of initial and end within the loop. Therefore, the subsequent iterations of the loop are using the same initial and end values, resulting in the same calculations being performed each time. To generate different values for x and f in each iteration, you should move the assignment of initial and end inside the loop By moving the assignment of end inside the loop, you will ensure that a different end value is used for each iteration, resulting in different calculations and different values for x and f.
29th May 2023, 4:18 AM
Jared
Jared - avatar
0
Vinay Sharma put the entire thing in a function. Then it is easier to try it multiple times and compare the effect of a large loop value against a small one. https://code.sololearn.com/c3CyffLTDIdL/?ref=app
29th May 2023, 5:14 AM
Bob_Li
Bob_Li - avatar