dimanche 15 mai 2016

Generate a random number which is far enough from another number

Let x, range, d be integers. We'd like to generate a number y, such that

  • 1 <= y <= range
  • abs(x-y) >= d

One idea I came up with is to generate some smaller range and then make some adjustments to handle the numbers which too close to x. But that's really tedious.

Is there any better way to do it?




Aucun commentaire:

Enregistrer un commentaire