A Ruby gem for probability-driven DSLs and dice rolls.
チャンス gives you cool ways to write code with probabilities, dice rolls, and randomness.
It comes with a natural-language DSL for describing likelihoods (probably, rarely, always, etc.) and utility methods like dice rolls (d6, d20, …).
- 🎲 Dice rolls:
d6,d20,d100and even (dice notation). - 📊 Probability DSL: Write code like:
probably { puts "this happens ~60% of the time" } rarely { puts "this almost never happens" } always { puts "this always happens" }
- 🌍 Globally available by default – no need to include anything. - (Can be disabled with Chansu.disable_globals!)
- 🕹️ Useful for games, simulations, generative art, or just spicing up your Ruby scripts.
Add this line to your application's Gemfile:
gem 'chansu'Then execute
bundle installOr just do
gem install chansuchance # true/false with 50% chance
chance(10) # 10% chance
chance(0.65) # 60% chance
chance("38%") # 38% chanceIt also receives a block
chance(40) {
puts "There's a 40% chance this was printed"
}You are provided with two methods that take a block and execute it until it either success or fails
until_success {
p "this will be printed until it is successfull!"
} #returns the number of attempts it took
until_failure(chance: 0.1, max_attempts: 25) {
p "this will be printed until it fails or hits 25 attempts!"
} #returns the number of attempts it tookKeep in mind the arguments work for both methods
require "chansu"
probably { puts "Hello, world!" } # ~60% chance
rarely { puts "Surprise!" } # ~10% chance
always { puts "Guaranteed!" } # 100%| Method | Probability | Example |
|---|---|---|
always |
100% | always { puts "Guaranteed!" } |
certainly |
90% | certainly { puts "Very likely" } |
almost_always |
85% | almost_always { puts "Pretty often" } |
usually |
80% | usually { puts "Most of the time" } |
often |
75% | often { puts "Happens often" } |
likely |
70% | likely { puts "Quite probable" } |
frequently |
65% | frequently { puts "Fairly frequent" } |
probably |
60% | probably { puts "More often than not" } |
more_often_than_not |
60% | more_often_than_not { puts "Similar to probably" } |
maybe |
50% | maybe { puts "Flip a coin" } |
possibly |
40% | possibly { puts "Could happen" } |
not_often |
35% | not_often { puts "Less common" } |
unlikely |
25% | unlikely { puts "Not very likely" } |
with_low_probability |
15% | with_low_probability { puts "Long shot" } |
rarely |
10% | rarely { puts "Almost never" } |
never |
~0% 😉 | never { puts "Basically never" } |
require "chansu"
coin # :tails or :heads
dice # 2
dice(42) # 31
dice(15, 3) # [15, 9, 15]
d6 # 6
d20 # 11
d100 # 90
dice("2d10") # {:rolls=>[9, 5], :total=>14}
dice("3d8+5") # {:rolls=>[3, 3, 4], :total=>15}
roll # is an alias of diceBy default, all DSL methods are available globally If you want to remove them from your global namespace, disable them with:
Chansu.disable_globals!And include them in your own modules/classes:
class MyClass
include Chansu
endClone and install dependencies:
bundle installRun tests
rake testBuild gem
gem build
Bug reports and pull requests are welcome on GitHub at https://github.com/escalderong/chansu Feel free to suggest new probability words, dice, or randomness helpers!
The gem is available as open source under the terms of the MIT License.