 • August 14, 2022, 09:21:11 PM

### Author Topic:  NON-Random walk, but based on Pi  (Read 160 times)

0 Members and 1 Guest are viewing this topic.

#### youhn ##### NON-Random walk, but based on Pi
« on: May 25, 2022, 03:00:00 PM »
To get a deterministic walk based on Pi, I tried the following:

1. Take the value of Pi as input to calculate the "random" step in either horizontal or vertical direction.
2. For each direction, apply the following operations with different parameters: addition, product, exponent.
3. Keep only the decimals (semi random value between 0 and 1) and scale to desired step size on the grid.
4. Add color value to the position and repeat the above from the current position on the grid.

Though running the code it seems to slow down after only a few iterations. I'm no expert in Python, so could use some help.

Example of the code:
Code: [Select]
import matplotlib.pyplot as pltimport mathimport numpy as npimport random# Imitation of random walk, but deterministic. def linspace(start, stop, num):    return [start + (stop - start) / (num - 1) * k for k in range(num)] def zeros(rows, cols):    return [[0 for j in range(cols)] for i in range(rows)] n = 40 # iterationsframes = 180width =  600height = 600T = np.zeros(shape=(height, width))time = linspace(0.0, 2 * np.pi, frames)a = int(height*2/3)b = int(width*2/3)halfstep = 7p = 1.24q = 1.76for m, dt in enumerate(time):    for k in range(n):        T[a][b] = T[a][b] + 36 + int(m*2)            # Increase color value on map        pia = np.pi * p * (m+k+1)%120 + dt*3    # variations on Pi        pib = np.pi * q * (m+k+2)%173 + dt*2 + int(m/5)        pia = np.power(pia, 1 + m/64)        pib = np.power(pib, 1 + m/64)        ar = int((pia - np.floor(pia)) * 2*halfstep - halfstep)   # calculate step        br = int((pib - np.floor(pib)) * 2*halfstep - halfstep)        a = (a + ar)%height        b = (b + br)%width         filename = "randrot" + str(m).zfill(3) + ".png"    print(filename)    plt.imshow(T, cmap=plt.cm.twilight_shifted)    plt.axis('off')    plt.savefig(filename, dpi=320, bbox_inches="tight", pad_inches=0)
Attached image of previous version of the code.

#### claude ##### Re: NON-Random walk, but based on Pi
« Reply #1 on: May 25, 2022, 04:12:02 PM »
my untested guess is that each frame you add an image to the plot, so saving frame m needs to render and save m images, resulting in total time O(m^2)

perhaps add plt.clf() into the inner loop? documentation: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.clf.html#matplotlib.pyplot.clf

EDIT: confirmed, appending "    plt.clf()" to the source code and it completes 180 frames in 50 seconds on my desktop, after 50 seconds the original code is on frame 22 and spewing exception backtraces from frame 11...
« Last Edit: May 26, 2022, 01:30:15 PM by claude, Reason: confirmed fix »

#### youhn ##### Re: NON-Random walk, but based on Pi
« Reply #2 on: May 29, 2022, 05:57:54 PM »
Thanks! That does speed up things a lot.

Next idea is how to combine nice patterns smoothly with (semi)randomness. Using a fixed sequence this should be not too hard. Perhaps I can use a wave shape to switch between pure random VS strict pattern over time. I image this would look like a spider silk at micro scale:

https://www.alamy.com/stock-image-scanning-electron-microscope-sem-micrograph-showing-spiders-silk-including-166850921.html

And maybe add some color like the +/-10 year old paper I just (re?)found:

https://carmamaths.org/resources/jon/numtools.pdf

#### youhn ##### Re: NON-Random walk, but based on Pi
« Reply #3 on: June 14, 2022, 08:51:57 PM »

Reversed non-random walk using Pi.

### Similar Topics ###### Mandelbulber 2.24 - Random OpenCL Error

Started by Voulxe on Mandelbulber

2 Replies
520 Views January 09, 2021, 08:35:24 AM
by Voulxe ###### Random spike minibrots

Started by pauldelbrot on Fractal Mathematics And New Theories

6 Replies
352 Views June 15, 2021, 08:10:44 PM
by youhn ###### Random Ellipsoid Packing

Started by hobold on Share a fractal

23 Replies
929 Views December 24, 2021, 12:27:51 AM
by hobold ###### Experiment - Using A Random Number Generator To Choose Fractals

Started by Tas_mania on Mandelbulber

2 Replies
905 Views December 22, 2019, 12:16:12 PM
by daudvyd ###### Random fractals and GPU rendering via commandline or Python?
8 Replies
842 Views December 25, 2019, 06:47:35 PM
by buddhi