|
|
|
|
|
by anonymoushn
3312 days ago
|
|
Arrays starting at 1 and being closed intervals is mostly just worse than starting at 0 and being half-open intervals. Here's an expression to take an integer b and "mod it" into an interval from a to c, given the normal convention from C++ or Python or whatever where intervals are [a, c): (b-a)%(c-a)+a
And here it is in the lua convention where intervals are [a,c]: (b-a)%(c-a+1)+a
This is a generalization of what you'd do if you got a really big random integer and you wanted to use it to pick from an array. For that, normally you'd write n % array.length
but in Lua you should be careful to write (n % #array) + 1
You can read some other opinion about this here https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E... |
|