0

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

4 Answers

+ 6

p is random, but it generates only a random value once. Shouldn't p be in the loop?

+ 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.

+ 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.

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