Calling Random Numbers
#1
I'm working on building a D&D styled battle system, and i need to know how to call random numbers from a specific range. for example, to hit with an attack, the basic formula goes like this:

if d20 + Actor strength mod + BAB > target AC
Hit
Else
Miss

I know this isn't proper code, but i'm still working on it. anyways, where it says d20, i need it to call a random number from 1-20. i'll add Critical functionality later, but for now, this is it.


Reply }
#2
HOWL!!!!

Heyoo, it's the Resident Werewolf to offer the Resident Wiccan a little dice script. And it has all your classic bag o' dice values from the 4-sider right up to the full 20 sided die.Winking


Code:
#==============================================================================
# ** The Dice Module
#------------------------------------------------------------------------------
#    by DerVVulfman
#    version 1.0
#    09-06-2013
#    RGSS / RPGMaker XP
#------------------------------------------------------------------------------
#
#  To use as a basic call, use the command:   result = Dice.d20(3)  to generate
#  a dice_roll of 3 20-sided dice.
#
#  Or, you may wish  to include this entire module  within a class  such as the
#  Game_Battler class.   To do so,  you would use an include statement to allow
#  all methods within to be used with ease.  This would appear as:
#
#      class Game_Battler
#        include Dice
#        #--------------------------------------------------------------------
#        # * Public Instance Variables
#        #--------------------------------------------------------------------
#        attr_reader   :battler_name             # battler file name
#
#  After that,  your calls within that class  would be simple indeed.  It would
#  merely be a case of using     result = d6(3)    to generate a classic '3-18'
#  bell curve common to Advanced Dungeons and Dragons games.
#
#==============================================================================


module Dice
  # Make methods within function
  module_function
  #--------------------------------------------------------------------------
  # * d4
  #     qty : quantity of dice rolled
  #--------------------------------------------------------------------------  
  def d4(qty=1)
    return dice_roll(4, qty)
  end
  #--------------------------------------------------------------------------
  # * d6
  #     qty : quantity of dice rolled
  #--------------------------------------------------------------------------  
  def d6(qty=1)
    return dice_roll(6, qty)
  end
  #--------------------------------------------------------------------------
  # * d8
  #     qty : quantity of dice rolled
  #--------------------------------------------------------------------------  
  def d8(qty=1)
    return dice_roll(8, qty)
  end
  #--------------------------------------------------------------------------
  # * d10
  #     qty : quantity of dice rolled
  #--------------------------------------------------------------------------  
  def d10(qty=1)
    return dice_roll(10, qty)
  end
  #--------------------------------------------------------------------------
  # * d12
  #     qty : quantity of dice rolled
  #--------------------------------------------------------------------------  
  def d12(qty=1)
    return dice_roll(12, qty)
  end
  #--------------------------------------------------------------------------
  # * d20
  #     qty : quantity of dice rolled
  #--------------------------------------------------------------------------  
  def d20(qty=1)
    return dice_roll(20, qty)
  end
  #--------------------------------------------------------------------------
  # * Rolling the Dice
  #     dice : type of dice rolled
  #     qty  : quantity of dice rolled
  #--------------------------------------------------------------------------  
  def dice_roll(dice, qty)
    # Reset the result
    result = 0
    # Ensure no value less than 1
    qty = 1 if qty < 1
    # Cycle through the dice
    for i in 1..qty.to_i
      # Add the random dice rolls
      result += rand(dice)+1
    end
    # Return the result
    return result
  end
end

I hope you like it. Now you have little commands to actually make a thing like...


Code:
hit_result = false
hit_result = true if Dice.d20 + attacker.str > target.ac
* Meh, I need my old Dungeon Master's Guide to be accurate. What the heck is BAB ??

or

Code:
strength_roll = Dice.d6(3)

The Dice.d20 command generates your d20 dice roll, and the Dice.d6(3) is equivalent to rolling 3 six-siders
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)

[Image: QrnbKlx.jpg]
[Image: sGz1ErF.png]    [Image: liM4ikn.png]    [Image: fdzKgZA.png]    [Image: sj0H81z.png]
[Image: QL7oRau.png]    [Image: uSqjY09.png]    [Image: GAA3qE9.png]    [Image: 2Hmnx1G.png]    [Image: BwtNdKw.png%5B]
  Above are clickable links
Reply }
#3
If you had read the RPGm XP help manual you would have known that rand(n) function can do exactly what you want. Well almost. Winking

And VVulfy, that is a little too much computational overhead for my tastes (and I've never seen a die numbered 0 to sides-1). I'd do it like this:

Code:
module Roll
    #--------------------------------------------------------------------------
    # d - simulates a dice roll (num: 1 to sides)
    #    *sides : how many sides the dice has
    #---------------------------------------------------------------------------
    def d(sides)
        return rand(sides)+1
    end
end

Then rolls are just:
Code:
strength_roll = Roll.d(20)
Reply }
#4
Given that he requested one for a Dungeons and Dragons styled system, he would be more familiar with 4sided dice, 6 siders, 8siders and so forth. Hence, my version was designed to use method names that replicated the AD&D dice rolls, and include the number of dice at the same time. The call of ...
Code:
strength_roll = Dice.dice_roll(6, 3)

...would match that of the Dice.d6(3). It rolls a six-sided die 3 times. But the Dice.d6 command (and others included) are meant to make it easier to use. I went with an easy-to-use gamer mentality for d4, d6, d8, d10, d12 and d20 rolls.

And call it an old-school methodology that I grew up with that reinitialized values ( the result = 0 ) as well as a mentality to include failsafe routines.
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)

[Image: QrnbKlx.jpg]
[Image: sGz1ErF.png]    [Image: liM4ikn.png]    [Image: fdzKgZA.png]    [Image: sj0H81z.png]
[Image: QL7oRau.png]    [Image: uSqjY09.png]    [Image: GAA3qE9.png]    [Image: 2Hmnx1G.png]    [Image: BwtNdKw.png%5B]
  Above are clickable links
Reply }
#5
I personally think
Code:
thing = Roll.d(20) + Roll.d(20)
makes more sense. It's self-documenting.

Plus, no matter what way you do it, it is still 'rand(n)+1' and not 'rand(n)' to get a dice roll. For example, rand(6) returns a number from 0-5. rand(6)+1 returns a number from 1-6 as required by dice.
Reply }
#6
Oh, I see what you mean about rand +1. Um, I caught that at 1-ish (about when you posted. I changed it in the script to
Code:
result += rand(dice)+1
.... I guess about the time you posted. Confused

And wouldn't that be
Code:
thing = Roll.d(20) + (Roll.d(20) +2
for two values of 1-20 added together?

With mine, it would be
Code:
thing = Dice.d20(2)
Shorter over the long run.
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)

[Image: QrnbKlx.jpg]
[Image: sGz1ErF.png]    [Image: liM4ikn.png]    [Image: fdzKgZA.png]    [Image: sj0H81z.png]
[Image: QL7oRau.png]    [Image: uSqjY09.png]    [Image: GAA3qE9.png]    [Image: 2Hmnx1G.png]    [Image: BwtNdKw.png%5B]
  Above are clickable links
Reply }
#7
Nope! Just
Code:
thing = Roll.d(20) + Roll.d(20)
Because I defined d to be a random number from 1 to n.

Either way works.
Reply }
#8
Oooh. Guess I mis-saw yours just as you musta seen my pre-edit version (ironically when I was editing it?). :P
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)

[Image: QrnbKlx.jpg]
[Image: sGz1ErF.png]    [Image: liM4ikn.png]    [Image: fdzKgZA.png]    [Image: sj0H81z.png]
[Image: QL7oRau.png]    [Image: uSqjY09.png]    [Image: GAA3qE9.png]    [Image: 2Hmnx1G.png]    [Image: BwtNdKw.png%5B]
  Above are clickable links
Reply }


Possibly Related Threads…
Thread Author Replies Views Last Post
   Making Forest Maps with the Random Map Generator ShadowIce 16 22,003 02-02-2014, 08:18 PM
Last Post: ShadowIce
   Help Making Random Maps ShadowIce 0 3,263 02-02-2010, 03:28 PM
Last Post: ShadowIce
   Calling Scripts aistinger 3 5,902 12-03-2009, 01:41 AM
Last Post: aistinger
   Random Monster Groups Yin 25 31,088 08-18-2009, 06:04 AM
Last Post: DerVVulfman



Users browsing this thread: 1 Guest(s)