dimanche 25 février 2018

Check if value is within range otherwise calculate nearest value

I have these value:

let previous = 170
let min = 100
let max = 800

So what I´m doing is that I´m generating a random number between min and max, with these rules:

  • min not less than 100
  • max not more than 800
  • I only want to have +200 from previous or -200 from previous

So for the above example the random number should be between:
min: 100 and max 370.

Min is 100 because min can´t be less than 100 and max is basically previous: 170 + 200.

I tried the "pattern-match" operator ~=: but it did not feel like the right component to do here.

Any ideas of how to solve this?




Aucun commentaire:

Enregistrer un commentaire