diff options
author | Nat Lasseter <user@4574.co.uk> | 2021-02-27 23:02:10 +0000 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2021-02-27 23:02:10 +0000 |
commit | 1aa60851a50376edc7d3b2b3dd2c7c41fe3b940c (patch) | |
tree | 64d53b445c70ccb5ab2605acf2324f2ba286b78f /iada.rb | |
parent | e662264ab6ecbb2b20bfdc8fc634cf24036cb612 (diff) |
Will this increase the uniformity of rand? We may never know. It damn well shouldn't.
Diffstat (limited to 'iada.rb')
-rwxr-xr-x | iada.rb | 18 |
1 files changed, 11 insertions, 7 deletions
@@ -2,6 +2,10 @@ require 'discordrb' +def make_roll(dX = 100) + rand(100_000_000 * dX) % dX +end + def human_result(name, roll, vs, fr, prefix = "") case roll when 0 @@ -39,7 +43,7 @@ bot = Discordrb::Commands::CommandBot.new token: ENV['DISCORD_TOKEN'], client_id bot.command(:vs, description: "Roll a d100 (0-99) against a target number, with optional comment.") do |event, vs, *fr| vs = vs.to_i - roll = rand(100) + roll = make_roll last_roll[event.author.id] = [roll, vs, fr] @@ -54,7 +58,7 @@ bot.command(:again, description: "Repeat your last vs roll.") do |event| if roll.nil? res = "You need to roll something first, #{event.author.display_name}!" else - roll = rand(100) + roll = make_roll last_roll[event.author.id] = [roll, vs, fr] res = human_result(event.author.display_name, roll, vs, fr, "Repeat: ") @@ -95,11 +99,11 @@ end bot.command(:init, description: "Roll a d10 (1-10) and add an init modifier, with two tie-break d10 (0-9) rolloffs.") do |event, mod| mod = mod.to_i - roll = rand(10) + 1 + roll = make_roll(10) + 1 tot = roll + mod - d1 = rand(10) - d2 = rand(10) + d1 = make_roll(10) + d2 = make_roll(10) res = "#{event.author.display_name} rolled `#{roll}` and scored `#{tot}.#{d1}#{d2}` for initiative." puts res @@ -111,13 +115,13 @@ bot.command(:unitest, description: "Perform a uniformity test for peace of mind. u = t / 100 a = Array.new(100, 0) - t.times { a[rand(100)] += 1 } + t.times { a[make_roll] += 1 } d2 = a.map { |o| (o - u) ** 2 } s2 = d2.sum / 100.0 s = s2 ** 0.5 - res = "#{event.author.display_name} requested a uniformity test. #{t} dice were rolled and counted. The standard deviation was #{"%.2f" % s}, which means 68% of the possible outcomes each appeared within #{s.ceil} of the expected #{u} times, 95% within #{(s*2).ceil}, and 99.7% within #{(s*3).ceil}." + res = "#{event.author.display_name} requested a uniformity test. #{t} d100s were rolled and counted. The standard deviation was #{"%.2f" % s}, which means 68% of the possible outcomes each appeared within #{s.ceil} of the expected #{u} times, 95% within #{(s*2).ceil}, and 99.7% within #{(s*3).ceil}." puts res res end |