#==============================================================================|
#  ** DoubleX RMVXA rand Edit v1.01a                                           |
#------------------------------------------------------------------------------|
#  * Changelog                                                                 |
#    v1.01a(GMT 1100 5-4-2014):                                                |
#    - Added Integer_Rand_Partition, Test_RNG_Times and Float_Rand_Partition   |
#    v1.00a(GMT 0100 5-4-2014):                                                |
#    - 1st version of this script finished                                     |
#------------------------------------------------------------------------------|
#  * Author                                                                    |
#    DoubleX                                                                   |
#------------------------------------------------------------------------------|
#  * Terms of use                                                              |
#    None other than not claiming this script as created by anyone except      |
#    DoubleX or his alias                                                      |
#------------------------------------------------------------------------------|
#  * Prerequisites                                                             |
#    Scripts:                                                                  |
#    - none                                                                    |
#    Knowledge:                                                                |
#    - Basic RNG knowledge                                                     |
#------------------------------------------------------------------------------|
#  * Functions                                                                 |
#    - Sets the number of times and partitions the RNG being run per rand call |
#------------------------------------------------------------------------------|
#  * Manual                                                                    |
#    To use this script, open the script editor and put this script into an    |
#    open slot between ▼ Materials and ▼ Main. Save to take effect.            |
#------------------------------------------------------------------------------|
#  * Compatibility                                                             |
#    Scripts aliasing or rewriting method rand under module Kernel may have    |
#    compatibility issues with this script                                     |
#    Place this script above those aliasing any of these methods if possible   |
#==============================================================================|

$imported = {} if $imported.nil?
$imported["DoubleX RMVXA rand Edit"] = true

#==============================================================================|
#  ** You only need to edit this part as it's about what this script does      |
#------------------------------------------------------------------------------|

module DoubleX_RMVXA
  module Rand_Edit

#------------------------------------------------------------------------------|
#  * (v1.01a+)Integer_Rand_Partition, default = true                           |
#    - RNG will have max partitions, max being the arguement of rand()         |
#    - Larger Integer_Rand_Partition generally means less random to the number |
#      generated by rand but more resources needed(mainly time) to run it      |
#------------------------------------------------------------------------------|
  Integer_Rand_Partition = true

#------------------------------------------------------------------------------|
#  * (v1.01a+)Test_RNG_Times, default = 0                                      |
#    - Test the performance and settings of this script by running rand        |
#      Test_RNG_Times times and displaying its min, max and mean afterwards    |
#    - The results of each rand can also be seen in the console of RMVXA       |
#    - Setting Test_RNG_Times as 0 will disable this feature completely         |
#------------------------------------------------------------------------------|
  Test_RNG_Times = 0

#------------------------------------------------------------------------------|
#  * (v1.01a+)Float_Rand_Partition, default = 10                               |
#    - RNG will be run under Float_Rand_Partition equal-sized partitions       |
#    - No partition will be picked twice before all partitions are picked      |
#    - Larger Float_Rand_Partition generally means less random to the number   |
#      generated by rand but more resources needed(mainly time) to run it      |
#------------------------------------------------------------------------------|
  Float_Rand_Partition = 10

#------------------------------------------------------------------------------|
#  * Rand_Times, default = 1                                                   |
#    - RNG will be run by rand Rand_Times times(returning mean afterwards)     |
#    - Larger Rand_Times generally means less random to the number generated by|
#      rand but more resources needed(mainly time) to run it                   |
#------------------------------------------------------------------------------|
  Rand_Times = 1

#------------------------------------------------------------------------------|
#  * Notes                                                                     |
#    - Float_Rand_Partition is inside the Rand_Times loop, meaning that rand   |
#      will pick Rand_Times partitions and return the mean afterwards per call |
#    - Mathematically speaking, randomness = f(Rand_Times) isn't decreasing but|
#      it should show an overall downward trend and approach to 0 in general   |
#------------------------------------------------------------------------------|

  end # Rand_Edit
end # DoubleX_RMVXA

#==============================================================================|

#==============================================================================|
#  ** You need not edit this part as it's about how this script works          |
#------------------------------------------------------------------------------|

module Kernel

  #----------------------------------------------------------------------------|
  #  Alias method: rand                                                        |
  #----------------------------------------------------------------------------|
  alias rand_edit rand
  def rand(max = 0)
    # This part is rewritten by this script to run the RNG for Rand_Times times with each partition
    partition = max.abs >= 1 ? DoubleX_RMVXA::Rand_Edit::Integer_Rand_Partition ? max.to_i.abs : 1 : DoubleX_RMVXA::Rand_Edit::Float_Rand_Partition.to_f
    @partition_hash_rand_edit = {} if !@partition_hash_rand_edit
    rng = 0
    DoubleX_RMVXA::Rand_Edit::Rand_Times.times do
      @partition_hash_rand_edit[partition] = [] if !@partition_hash_rand_edit[partition] || @partition_hash_rand_edit[partition].size >= partition
      part = rand_edit(partition.to_i)
      part = rand_edit(partition.to_i) while !@partition_hash_rand_edit[partition].empty? && @partition_hash_rand_edit[partition].include?(part)
      @partition_hash_rand_edit[partition].push(part)
      rng += (rand_edit(max) + part * [max, 1].max) / partition
    end
    rng /= DoubleX_RMVXA::Rand_Edit::Rand_Times
    #
  end # rand

end # Kernel

if DoubleX_RMVXA::Rand_Edit::Test_RNG_Times > 0
  min = 1
  max = sum = 0
  DoubleX_RMVXA::Rand_Edit::Test_RNG_Times.times do
    rng = rand
    p(rng)
    sum += rng
    min = rng if min > rng
    max = rng if max < rng
  end
  msgbox_p("min", min, "max", max, "mean", sum / DoubleX_RMVXA::Rand_Edit::Test_RNG_Times)
end

#==============================================================================|