Computationally inexpensive pseudorandom 2d number

371 Views Asked by At

I am trying to calculate a random position on a grid in a very inexpensive manner for a gpu kernel however I can not quite figure out how to get it right.

You see the function that generates the random coordinate can have only one input "t". t is an integer and it has a range of 0-250. This restraint bars me from using the usual gpu function I have heard of.

What I have been playing with is a parametric function i remember from algebra where x=sin(ax) and y=cos(by) as you change the values of a and b this equation generates an intresting shape. It would seem as you increase the least common multiple of a and b that the shape becomes more complex.

enter image description here

I figure if I can get this function to be "complex" enough any random t on the function should generate a "random" point.

My problem is I don't have enough knowlege of this function to customize it for my scenario. To make this function seem random the function needs to be complex. I have found values of a and b such as 55.7 and 33.9 that do generate a sorta complex function (not quite complex enough and I cant figure out how to determine an ideal complexity with these values) however when I apply them they tend to have a bias of some sort. In this case the x axis was just about perfectly distributed however the y axis ended up being grouped into obvious bars.

I believe this is because t is an integer.

How can I avoid x and y axis groupings with my values of a and b?

It is also possible that I am going about this entirely the wrong way and I should pick a different function.

1

There are 1 best solutions below

2
On BEST ANSWER

Well, at first, happy New Year! I must admit that this one has got me down writing a little bit, but is seems a very interesting problem. As I see it - correct me if you had something else in mind - a notion of "complexity" for your problem would be as follows:

Find $(a,b)\in\mathbb{Z}_+^2$ such that the self-intersecting points of the curve $c(t)=(\sin(at),\cos(bt))$ are "almost" uniformly distributed in a rectangle $[-\delta,\delta]\times[-\delta,\delta]\subseteq[-1,1]\times[-1,1]$, $\delta>0$.

I chose only the self-intersecting points of $c$ just for reasons of simplicity; when they seem to be uniformly distributed, the plane is almost "gridified" - seems to be a grid - by the plot of $c$. For instance, for $a=97$ and $b=98$ the plot of $c$ is:

enter image description here

Now, at first we sould be able to, at least describe, the self-intersecting points of $c$. So let $$I_c=\{c(t)|\exists\ s\in\mathbb{R}\text{ with }c(t)=c(s),\ t\in\mathbb{R}\}$$

So, the points in $I_c$ should be exactly the solutions of the following system of equations: $$c(t)=c(s),\ t\neq s\tag{1}$$ Let us play around with $(1)$ a little bit: $$\begin{align*} c(t)=c(s)&\Leftrightarrow\left\{\begin{array}{c} \sin(at)=\sin(as)\\ \text{and}\\ \cos(bt)=\cos(bs) \end{array}\right\}\\ &\Leftrightarrow\left\{\begin{array}{c} at=2k\pi+as\text{ or }at=2k\pi+\pi-as\\ \text{and}\\ bt=2k\pi+bs\text{ or }bt=2k\pi-bs\\ \end{array}\right\}\\ &\Leftrightarrow\left\{\begin{array}{c} t=2k\frac{\pi}{a}+s\text{ or }t=(2k+1)\frac{\pi}{a}-s\\ \text{and}\\ t=2k\frac{\pi}{b}+s\text{ or }t=2k\frac{\pi}{b}-s\\ \end{array}\right\}\\ \end{align*}$$

From this, four "sub-systems" occur: $$\begin{align*} \left\{\begin{array}{c} t=2k\frac{\pi}{a}+s\\ \text{and}\\ t=2k\frac{\pi}{b}+s\\ \end{array}\right\}\tag{1.1}\\ \left\{\begin{array}{c} t=2k\frac{\pi}{a}+s\\ \text{and}\\ t=2k\frac{\pi}{b}-s\\ \end{array}\right\}\tag{1.2}\\ \left\{\begin{array}{c} t=(2k+1)\frac{\pi}{a}-s\\ \text{and}\\ t=2k\frac{\pi}{b}+s\\ \end{array}\right\}\tag{1.3}\\ \left\{\begin{array}{c} t=(2k+1)\frac{\pi}{a}-s\\ \text{and}\\ t=2k\frac{\pi}{b}-s\tag{1.4}\\ \end{array}\right\}\\ \end{align*}$$

System $(1.1)$ implies that $a=b$ which is a dump case, since, then, $c$ is a circle. Also, system $(1.4)$ implies that $(2k+1)b=2ka$ for some $k\in\mathbb{Z}_+$, which is a very specific case - b should be odd and many more restrictions - that will not bother us much - this will be clear in the following lines. So, we are left with systems $(1.2)$ and $(1.3)$ that give the following solutions for $t$: $$\begin{align*} t&=k\pi\frac{a+b}{ab}\\ t&=k\pi\frac{a+b}{ab}+\frac{\pi}{2a} \end{align*}$$ and for $s$: $$\begin{align*} s&=k\pi\frac{a-b}{ab}\\ t&=k\pi\frac{a-b}{ab}-\frac{\pi}{2a} \end{align*}$$ where, in all the above, $k\in\mathbb{Z}$ (well, we should exclude $k=0$ but, later on, we will not take into account these points).

Wait, what?! Aren't there infinitely many integers? So, are there infinitely many self-intersection points of $c$? Well, no, there are finitely many elements of $I_c$, and this can be proved as follows; let $d=\gcd(a,b)$ be the greatest common divisor of $a,b$. Then curve $c$ has a periodicity of $T=\frac{2\pi}{d}$. Indeed, let us calculate $$c\left(t+\frac{2\pi}{d}\right)=\left(\sin\left(at+2\pi\frac{a}{d}\right),\cos\left(bt+2\pi\frac{b}{d}\right)\right)=(\sin(at),\cos(bt))=c(t)$$ since $d|a$ and $d|b$. There is, also, no number $T'<T$ that has this property for $c$, due to the maximality of $d$ and the fact that $\sin$'s and $\cos$'s period is exactly $2\pi$.

So, we can restrict $c$ over the interval $\left[0,\frac{2\pi}{d}\right]$.

At this point, a note of yours made me have a deeper look into the way $e:=\mathrm{lcm}(a,b)$ estimates the number of elements of $I_c$. I will, intentionally let aside the majority of the calculations, since they are similar to the previous ones and I will just sketch the idea: Fix a point in $I_c$; so it must be of the form $(x,y)=c(t)=(\sin(at),\cos(bt))$. Now, cosnider a line parallel to the $x-$axis. Every intersection of $c$ with that line is of the form $(\sin(at),\cos(at'))$ for a proper $t'$. But, since that point is also a self-intersecting point of $c$, it should also be true that $c(t')\in I_c$ (this is where the calculations are ommitted). The same applies if we consider a line parallel to $y-$axis. So, at most all the pairs with numbers of the form described above, if we consider the sequence: $$t_k=\left\{\begin{array}{ll} \frac{k}{2}\pi\frac{a+b}{ab} & \text{if }k\equiv0\mod 2\\ \frac{k-1}{2}\pi\frac{a+b}{ab}+\frac{\pi}{2a} & \text{if }k\equiv1\mod 2 \end{array}\right.$$ then, every combination of terms of this sequence gives a point in $I_c$. Now, consider also that $c$ is of periodicity $T=\frac{2\pi}{d}$. So, we must have that: $$t_k\in\left[0,\frac{2\pi}{d}\right]$$ Approximately, this implies that: $$k\leq\frac{4ab}{a+b}\frac{1}{d}$$ So, an oversetimation - a vast one, indeed - would be that there can exist, at most: $$\left(\frac{4ab}{a+b}\frac{1}{d}\right)^2$$ points in $I_c$ - we may have a better estimation, but it is too tiring for our purposes. Now, to minimize this estimation, we should minimize the denominator and, especially, $d$ - since $ab$, eventually, "kills" $a+b$. So, the minimum value for $d$ is $1$ and, hence, we should choose numbers $a,b$ such that they are relatively prime. Note now, regarding $e$, that this is, as you noticed, directly connected with this estimation, since $ab=de$, so, minimizing $d$ maximises $e$.

Now, what about the distances of these points and their distribution on the plane? Well, as depicted in the following .gif (inform me if this does not open; it was quite large):

https://drive.google.com/file/d/1BWAvNGnkFqxpST8D5LB9GQmRkRwEw08G/view

where the green points are the ones generated by odd $t_k$'s and the purple are the even ones, $t_k$ gives us a nice way to find an estimation of the minimum distance between "consecutive" points in $I_c$. We will caclulate, at first $\lVert c(t_k)-c(t_{k+1})\rVert$. So, using known trigonometric formulas for the sum of sines and cosines, we have that: $$c(t_k)-c(t_{k+1})=(2\sin\theta_a\cos\phi_{k,a},2\sin\theta_b\sin\phi_{k,b})$$ where $$\begin{align*} \theta_a&=\frac{\pi}{2}\frac{a+b}{b}\\ \theta_b&=\frac{\pi}{2}\frac{a+b}{a}\\ \phi_{k,a}&=\frac{2k+1}{2}\pi\frac{a+b}{a}\\ \phi_{k,b}&=\frac{2k+1}{2}\pi\frac{a+b}{b}\\ \end{align*}$$ So, $$\lVert c(t_{k})-c(t_{k+1})\rVert=\sqrt{4(\sin^2\theta_a\cos^2\phi_{k,a}+\sin^2\theta_b\cos^2\phi_{k,b})}$$ Note now that, for $b\sim a$ we have that: $$\theta_a\sim\theta_b$$ and then: $$\lVert c(t_{k})-c(t_{k+1})\rVert\sim2|\sin^2\theta_a|$$ which means that the points of $I_c$ are almost at stable distance, for a large area around $O(0,0)$ (that area can be found case-specifically, for given $a,b$; it takes some time to find a general formula).

So, for example, for $b=a+2$ or $b=a+1$ (you may accuse me of incosistency, since $(1.4)$ and its solutions have been excluded, but, as it has been mentioned, there is not much difference, at the estimation level) and for $d=1$ one can plot a nice grid in a large area of the $[-1,1]\times[-1,1]$ square.

So, to conclude, choose $a,b$ rather large, with $\gcd(a,b)=1$ and $a\sim b$. Finally, here's a .gif

https://drive.google.com/open?id=1c3erBWKkhkSfehCETHgnamUNTwtxl0kB

with $b=a+1$ and $a$ from $50$ to somewhere aorund $500$ - in the end, it really had some hard times...