isaacg1 / pyth

Pyth, an extremely concise language. Try it here:
https://pyth.herokuapp.com/
MIT License
263 stars 57 forks source link

Improve random functions #166

Closed PurkkaKoodari closed 8 years ago

PurkkaKoodari commented 8 years ago

These commits improve the functionality of O.

Firstly, a function for O<float> and O<cmp> is added. The function generates a random number between 0 and the input for floats (works for positive & negative), and multiplies the input by a random number between 0 and 1 for complex.

Secondly, O<int> can now be used for very large inputs. Previously it would consume extra memory and, with large enough inputs just fail immediately due to the code using random.choice(urange(a)). Changing that to random.randrange(a) gets the same result without problems and extra memory consumption.

Finally, the handling of O<seq> is improved. The current implementation creates a useless copy of the sequence, which can be dropped as random.choice does not modify its argument.

(One thought could be implementing O<cmp> as complex(random.uniform(a.real), random.uniform(a.imag)), but I'm not sure the entire function will be used that much.)