DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Roger has posted 3 posts at DZone. View Full User Profile

Polyhedral Dice

  • submit to reddit
        // Allows a user to determine how many dice are being rolled and how many sides the dice have then totals the results.  This was designed for my personal use with Dungeons and Dragons since I do not own any physical dice.  Feel free to use this code and improve upon it.  Any suggestions are more than welcome.  I am a Ruby novice.

    puts "How many dice are you rolling?  "
    dice = gets.chomp
    puts "How many sides does each die have?  "
    sides = gets.chomp
    x = 0
    y = dice.to_i
    while x < y
            result = result.to_i + rand(sides.to_i) + 1
            x = x + 1
    puts "The number you rolled is " + result.to_s