mardi 3 mai 2016

Constraining random number generation in Python

I am trying to create a loop in Python with numpy that will give me a variable "times" with 5 numbers generated randomly between 0 and 20. However, I want there to be one condition: that none of the differences between two adjacent elements in that list are less than 1. What is the best way to achieve this? I tried with the last two lines of code, but this is most likely wrong.

for j in range(1,6):

    times = np.random.rand(1, 5) * 20
    times.sort()
    print times

    da = np.diff(times) 

    if da.sum < 1: break

For instance, for one iteration, this would not be good:

4.25230915 4.36463992 10.35915732 12.39446368 18.46893283

But something like this would be perfect:

1.47166904 6.85610453 10.81431629 12.10176092 15.53569052

Thank you very much in advance for your help!




Aucun commentaire:

Enregistrer un commentaire