Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Undefined method error

I wrote a script with a couple methods dealing with random numbers:
Ruby:
#==============================================================================

# ** Game_System

#------------------------------------------------------------------------------

#  This class handles data surrounding the system. Backround music, etc.

#  is managed here as well. Refer to "$game_system" for the instance of 

#  this class.

#==============================================================================

 

class Game_System

  attr_accessor :pseudorandom_seed

  alias old_initialize initialize

  #--------------------------------------------------------------------------

  # * Object Initialization

  #--------------------------------------------------------------------------

  def initialize

    @bgm_volume = 0

    @bgs_volume = 0

    @se_volume = 0

    @me_volume = 0

    @pseudorandom_seed = rand(18446744073709551614) + 1

    old_initialize

  end

 end

 

<span style="color:#000080; font-style:italic;">=begin

<span style="color:#000080; font-style:italic;">==============================================================================

<span style="color:#000080; font-style:italic;"> ●● Jeff's Utility functions

<span style="color:#000080; font-style:italic;">------------------------------------------------------------------------------

<span style="color:#000080; font-style:italic;"> JeffreytheGreen

<span style="color:#000080; font-style:italic;"> Version 1.0

<span style="color:#000080; font-style:italic;">==============================================================================

<span style="color:#000080; font-style:italic;">=end

 

module JeffUtilities

  #-------------------------------------------------------------------------

  # * Normal distribution

  #     Returns a floating-point number drawn from a

  #     normal distribution. If no *args, uses standard distribution

  #     

  #     mean  : mean of distribution

  #     stdev : standard deviation of distribution

  #-------------------------------------------------------------------------

  def normal(mean = 0, stdev = 1)

    w = 2

    while w >= 1 # Generate standard normal distribution

      x1 = 2.0 * rand() - 1.0

      x2 = 2.0 * rand() - 1.0

      w = x1 ** 2 + x2 ** 2

    end

    w = Math.sqrt((-2.0 * Math.log(w)) / w)

    w = w * stdev + mean #Convert to n(mean, stdev)

    return w

  end

  #-------------------------------------------------------------------------

  # * Pseudorandom number

  #     Returns a random number generated using the seed stored in 

  #     $game_system.pseudorandom_seed, updates 

  #     $game_system.pseudorandom_seed, and re-randomizes the random seed used

  #     by rand.

  #     

  #     max : the maximum of the pseudorandom number. If 0 will return a

  #           floating point number between 0 and 1

  #-------------------------------------------------------------------------

  def pseudorand(max = 0)

    srand($game_system.pseudorandom_seed) #set random seed

    bigrandom = rand(18446744073709551615) #generate random 64-bit integer

    if max == 0

      pseudorand = bigrandom / 18446744073709551615.0

    else

      pseudorand = bigrandom % (max + 1)

    end

    $game_system.pseudorandom_seed = bigrandom

    srand

    return pseudorand

  end

end
but whenever I call pseudorand or normal, I get an undefined method error. I also tried changing JeffUtilities to a class instead of module, but it didn't affect it.

I was testing it using p JeffUtilities::pseudorand() or p JeffUtilities::normal().

Any help would be greatly appreciated.
 
Are you supposed to be taking the square root of a negative number for the normal function?

The natural log of any number (greater than one) times a negative number will always be negative...
 
Yeah.. whenever creating methods for a module and you want to call them using: Module.method
Then you have to use "def self.method"

Another way to look at it is that
def method is local access only and
def self.method is the same method when accessed from the outside. This logic is also the same when dealing with classes.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top