Advertisement
minimite

random

Sep 13th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.22 KB | None | 0 0
  1. --True Random API for Minecraft mod ComputerCraft
  2. --By minimite
  3.  
  4. --For future notice, do NOT run this program manually (os.loadAPI is fine), or else a reboot is needed.
  5.  
  6.  
  7. local header = {
  8. ["User-Agent"] = "I'll leave the user-agent as this."
  9. }
  10.  
  11. function getQuota(ip)
  12. local ip = ip or nil
  13. if ip then
  14. local rand = http.get("http://www.random.org/quota?ip="..ip.."&format=plain", header)
  15. local result = rand.readAll()
  16. local result = tonumber(result)
  17. return result
  18. else
  19. local rand = http.get("http://www.random.org/quota?format=plain", header)
  20. local result = rand.readAll()
  21. local result = tonumber(result)
  22. return result
  23. end
  24. end
  25.  
  26.  
  27. function quotaCheck() --Used to make sure clients don't ban themselves from random.org by using negative quota
  28. local quota = tonumber(getQuota())
  29. if quota < 1 then
  30. error("Sorry, but you've used all of your quota! If you wonder what Quota is, it's to make sure people don't hog all the random requests, and I have installed this to ensure your IP doesn't ban itself from using negative quota. Come back tommorow to issue MAOR random requests!")
  31. end
  32. end
  33.  
  34. function number(min,max,amount)
  35. quotaCheck()
  36. local amount = amount or 1
  37. local min = min or 1
  38. local max = max or 10
  39. local rand = http.get("http://www.random.org/integers/?num="..amount.."&min="..min.."&max="..max.."&col=1&base=10&format=plain&rnd=new",header)
  40. local result = rand.readAll()
  41. return result
  42. end
  43.  
  44. function string(length,amount)
  45. quotaCheck()
  46. local amount = amount or 1
  47. local length = length or 8
  48. local rand = http.get("http://www.random.org/strings/?num="..amount.."&len="..length.."&digits=on&upperalpha=on&loweralpha=on&unique=on&format=plain&rnd=new", header)
  49. local result = rand.readAll()
  50. return result
  51. end
  52.  
  53. function ticket(amount)
  54. quotaCheck()
  55. local amount = amount or 1
  56. local rand = http.get("http://www.random.org/quick-pick/?tickets="..amount.."&lottery=5x59.1x35&format=plain", header)
  57. local result = rand.readAll()
  58. return result
  59. end
  60.  
  61. function byte(length,type)
  62. quotaCheck()
  63. local length = length or 10
  64. local type = type or d
  65. if type == "f" then
  66. error("Don't use F. It downloads the file, which we don't want.")
  67. end
  68. local rand = http.get("http://www.random.org/cgi-bin/randbyte?nbytes="..length.."&format="..type.."", header)
  69. local result = rand.readAll()
  70. if result == "illegal format specified" then
  71. error("That's not a format! Formats are: d, h, o, b")
  72. end
  73. return result
  74. end
  75.  
  76. function decimal(places,amount)
  77. quotaCheck()
  78. local places = places or 10
  79. local amount = amount or 1
  80. if places > 20 then
  81. error("Amount of places must be 1-20!")
  82. elseif places < 1 then
  83. error("Amount of places must be 1-20!")
  84. end
  85. local rand = http.get("http://www.random.org/decimal-fractions/?num="..amount.."&dec="..places.."&col=2&format=plain&rnd=new",header)
  86. local result = rand.readAll()
  87. return result
  88. end
  89.  
  90. function getLostQuota()
  91.  
  92. if not fs.exists(".quota") then
  93. local fuo = fs.open(".quota","w")
  94. fuo.writeLine(getQuota())
  95. fuo.close()
  96. end
  97. local fuo = fs.open(".quota","r")
  98. local current = tonumber(fuo.readAll())
  99. local result = current - getQuota()
  100. fuo.close()
  101. local fuo = fs.open(".quota","w")
  102. fuo.writeLine(tonumber(getQuota()))
  103.  
  104.  
  105.  
  106.  
  107. fuo.close()
  108. return result
  109. end
  110.  
  111. function time(count)
  112. quotaCheck()
  113. local count = count or 1
  114. local rand = http.get("http://www.random.org/clock-times/?num="..count.."&earliest=01%3A00&latest=23%3A59&interval=5&format=plain&rnd=new", header)
  115. local result = rand.readAll()
  116. return result
  117. end
  118.  
  119. function note(count)
  120. quotaCheck()
  121. local count = count or 1
  122. local rand = http.get("http://www.random.org/cgi-bin/jazz-scale?num="..count.."&key_c=on&key_g=on&key_d=on&key_a=on&key_e=on&key_b=on&key_fsharp=on&key_csharp=on&key_aflat=on&key_eflat=on&key_bflat=on&key_f=on&string_1=on&string_2=on&string_3=on&string_4=on&string_5=on&string_6=on&form_scale=on&type_major=on&type_minor_pent=on&type_major_pent=on&type_7th_pent=on", header)
  123. local result = rand.readAll()
  124. return result
  125. end
  126.  
  127. function securestring(length,count) --Nobody can see the result but you
  128. quotaCheck()
  129. local count = count or 1
  130. local length = length or 8
  131. local rand = http.get("http://www.random.org/passwords/?num="..count.."&len="..length.."&format=plain&rnd=new", header)
  132. local result = rand.readAll()
  133. return result
  134. end
  135.  
  136. function org()
  137. local result = "It's not like every function in this API connects to random.org or anything.."
  138. return result
  139. end
  140.  
  141. function date(yearstart,yearend,count)
  142. quotaCheck()
  143. local yearstart = yearstart or 2014
  144. local yearend = yearend or 2014
  145. local rand = http.get("http://www.random.org/calendar-dates/?num="..count.."&start_day=1&start_month=1&start_year="..yearstart.."&end_day=31&end_month=12&end_year="..yearend.."&mondays=on&tuesdays=on&wednesdays=on&thursdays=on&fridays=on&saturdays=on&sundays=on&display=7&format=plain&rnd=new", header)
  146. local result = rand.readAll()
  147. return result
  148. end
  149.  
  150. function gaussian(places,count,mean,stdev)
  151. quotaCheck()
  152. local mean = mean or 0.0
  153. local stdev = stdev or 1.0
  154. local count = count or 1
  155. local places = places or 10
  156. local rand = http.get("http://www.random.org/gaussian-distributions/?num="..count.."&mean="..mean.."&stdev="..stdev.."&dec="..places.."&col=2&notation=scientific&format=plain&rnd=new")
  157. local result = rand.readAll()
  158. return result
  159. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement