VeryAnts - probabilistic integer arithmetic for Ruby
VeryAnts is a library for replacing regular (i.e. deterministic) arithmetic
operations with equivalent (on average) probabilistic variants. This is done by
supplying a variance constant, which determines a range of possible results for
each operation. For instance, given a constant
x + y is
sum := x, chance := 1 / c (c * y) times do r := [new random number between 0 and 1] if r < chance sum := sum + 1 return sum
c = 3,
x = 5,
y = 2, you start with
x, and have a
1 to it six different times. On average, this would occur twice,
which would give you 7, the correct answer. Another nice property is that for
c = 1, addition works completely normally.
All of the other arithmetic operators (
%) have the same
two properties, namely that they give the correct result on average, and they
always give the correct result when
c = 1. Additionally, they also have the
less-easy-to-define property that a higher
c roughly correlates to higher
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install very_ants
- Require the gem
- Set the variance constant
- Try and (mostly) fail to get correct arithmetic results
5 + 7 == 12 # probably not, but who knows
Warning: weird things can occur if you're running this in a REPL. For instance,
pry, the arithmetic done internally by the interpreter seems to be
affected, so sometimes the line numbers will get screwed up or the interpreter
Why would you ever want to do this?
Why did you make it then?
I came up with the idea as a joke when talking to my roommate, and I thought it would be funny.
Isn't this is pretty slow way to do arithmetic?
Is efficiency really the reason you think this is a bad idea?
What does "Very Ants" mean?
It's a pun on the word "variance"; "vari-ance" sounds like "very ants". Also, it behaves really weirdly, or put another way, "very antsy".
...I hate you
The gem is available as open source under the terms of the MIT License.