RonanPlugins / BetterRTP

Official wiki of the BetterRTP plugin!
https://www.spigotmc.org/resources/36081/
MIT License
111 stars 86 forks source link

flatten random distribution #79

Closed DailyStruggle closed 3 years ago

DailyStruggle commented 3 years ago

The circle calculation trended towards the outer edge where min>0. A new donut calculation flattens the spatial distribution.

The new calculation follows a 2D spiral that turns once per integer step, with a length equal to the area within the donut. I2dPFCI

using maxrad = 4096 min = 0

it matches the old distribution image

using maxRad = 4096 min = 1024

there's an improvement around the donut hole image

python scripts used: before

import matplotlib.pyplot as plt
import numpy as np
import math
import random
import time
plt.style.use('seaborn-whitegrid')

plt.title('Before')

it = 5000
radius = 4096
centerRadius = 0
x = np.zeros(it)
z = np.zeros(it)
def randomSelect(i) :
    distance = centerRadius + (radius-centerRadius)*math.sqrt(random.uniform(0,1))
    rotation = random.uniform(0,1)*math.pi*2

    xLoc = int(distance * math.cos(rotation))
    zLoc = int(distance * math.sin(rotation))

    x[i] = xLoc
    z[i] = zLoc

start = time.time()
for i in range(it) :
    randomSelect(i)
stop = time.time()
print("time: ",stop-start)

plt.plot(x,z,'o',color='black')

plt.show();

after

import matplotlib.pyplot as plt'
import numpy as np
import math
import random
import time
plt.style.use('seaborn-whitegrid')

plt.title('After')

it = 5000
radius = 4096
centerRadius = 0
x = np.zeros(it)
z = np.zeros(it)
def randomSelect(i) :
    totalSpace = math.pi*(radius+centerRadius)*(radius-centerRadius)
    randSpace = random.randrange(0,int(totalSpace))

    distance = math.sqrt(randSpace/math.pi + centerRadius*centerRadius)
    rotation = (distance - int(distance))*math.pi*2

    xLoc = distance * math.cos(rotation)
    zLoc = distance * math.sin(rotation)

    x[i] = xLoc
    z[i] = zLoc

start = time.time()
for i in range(it) :
    randomSelect(i)
stop = time.time()
print("time: ",stop-start)

plt.plot(x,z,'o',color='black')

plt.show();
SuperRonanCraft commented 3 years ago

I like what ya did here, and I love the explanation of the change. thank you for your contribution! I will do some tests before the next update, but this will be very much appreciated for those looking for a more flat randomness across the "donut" random generation.

DailyStruggle commented 3 years ago

also, I built a basic logging/learning algorithm on top of this design for performance improvements and for optional unique placements and automatic region expansion.

https://youtu.be/PY6W7DikhAI

https://github.com/DailyStruggle/RTP/blob/main/src/main/java/leafcraft/rtp/tools/selection/TeleportRegion.java

I'm not sure how to work it in within this repo, but it's an interesting bonus feature of the design.