Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. -- extension of math lib
  2. local math = require "math"
  3.  
  4. math.clamp = function(value, lower, upper)
  5. if lower > upper then lower, upper = upper, lower end -- swap if boundaries supplied the wrong way
  6. return math.max(lower, math.min(upper, value))
  7. end
  8.  
  9. math.sign = function(value) -- Copyright 2013 Arman Darini
  10. if value > 0 then return 1; elseif value < 0 then return -1; else return 0; end
  11. end
  12.  
  13. math.round = function(value)
  14. return math.floor(value + 0.5)
  15. end
  16.  
  17. math.dice = function(dice, side)
  18. local value = 0
  19. for i = 1, dice do
  20. value = value + math.random(1, side)
  21. end
  22. return value
  23. end
  24.  
  25. -- Probability distribution function
  26. --
  27. -- if 'probabilityPower' is above 1, lower values will be more common than higher values
  28. -- if it's between 0 to 1, higher values will be more common than lower values.
  29. -- If it's 1, the results will be in a general randomness.
  30. math.randomP = function(min, max, probabilityPower)
  31. return math.floor(min + (max + 1 - min) * (math.pow(math.random(), probabilityPower)))
  32. end
  33.  
  34. -- @param number 0.0 - 1.0
  35. math.chance = function(chance)
  36. local value = math.random(0, 10000)
  37. if value/10000 <= chance then
  38. return true
  39. else
  40. return false
  41. end
  42. end
  43.  
  44. return math
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement