vendredi 1 mai 2020

Python: generate a list of integers which are not too close to one another

I'm trying to define a function that, given a length and integer, generates a list of that lenght of random integers which are at least seperated from one another by that given integer. An example:

>>> generate_distant_integers(5,2) 
[2,4,6,13,20]

A possibility I'm thinking of is to use the random library to add random integers to the list until the desired size is reached. Then I would introduce a check that loops over every integer and checks it is seperated enough from all integers from the list. However, I am wondering if there is no more efficient approach?




Aucun commentaire:

Enregistrer un commentaire