lazuscripts-utility/calc.moon
Paul Liverman III c615ea4b1f re-initialized
tl;dr: rewriting history of this repo because I have accidentally
included the history of OTHER repos in it due to either a bug with
git subtree or a misunderstanding of how it functions on my part
2018-03-04 11:41:07 -08:00

31 lines
711 B
Plaintext

import bytes from require "resty.random"
import byte from string
import floor from math
-- return a random number between min/max
random = (min, max) ->
unless max
max = min or 1
min = 0
a, b = byte bytes(2), 1, 2
c, d = byte bytes(2), 1, 2
value = a + b * 256 + c * 65536 + d * 16777216 -- 0 to 4294967296
range = max - min
if max == floor(max) and min == floor(min)
return floor value * range / 4294967296 + min
else
return value * range / 4294967296 + min
-- map a value within a range of numbers to another range
map = (min1, max1, value, min2, max2) ->
range1 = max1 - min1
range2 = max2 - min2
return ((value - min1) * range2 / range1) + min2
{
:random
:map
}