Jeff the Green
Member
I wrote a script with a couple methods dealing with random numbers:
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.
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
I was testing it using p JeffUtilities::pseudorand() or p JeffUtilities::normal().
Any help would be greatly appreciated.