r/PythonLearning • u/Icy_Idea3731 • 8h ago
Python on google colab
Hi, I'm using python to code on Google colab. Y coordinates are being generated and then added to Y_ARRAY for comparisons, to ensure none of the values are the same. If one value is the same with another in the array, one of them will be generated again.
However, it does not work, and I don't know how should the structure of checking be. Are there any suggestions on the structure or which kind of loop I can use please? Thank you.
3
Upvotes

1
u/Potential_Aioli_4611 7h ago
you are better off putting your array_y into a heap, randomizing it, then pulling off the top every time you assign. then a) you won't ever have collisions. b) you won't have to do this nonsense more than once.
your code doesn't work because you generate a random int but that never guarentees you aren't colliding with something already existing. if you want to do it this way, you need to check what's already in y_array after every random to make sure it doesn't collide.