Chinese Mid-Autumn Festival

Yesterday I came across this method to generate pseudo random numbers when reading about the quasicrystalline tilings. Perhaps it has been discovered by others long ago. If so, I appreciate it very much that you would inform me about the original source.

The algorithm can be coded in MATLAB as follows. Any irrational number can be used as the seed (the irrational unit length ir in this program). For example, I tried \sqrt{2} e (sqrt(2)*exp(1) in MATLAB). You may make it in your own flavor.

中秋快乐!

%If the ratio of the periods of two periodic lattice is irrational,
%they will never superimpose each other after the first point. Thus,
%the decimal part of a 1D periodic lattice with the unit length of an
%irrational number will not repeat as we probe unidirectionally along
%the lattice. From another perspective, if we fold such a 1D lattice
%into a 1D box of length 1, the lattice point will fill up the box
%except for those rational fraction points. This can be used to
%create pseudo random numbers between 0 and 1.

%Initially, we get the decimal part of an irrational number as the first
%number in the random series, use its last digit (semi-random due to the
%roundoff error) to jump to the next lattice point.

%Here we use pi as the irrational period of the lattice. We can use
%other irrational number

function [] = RandomFraction(p,n,ir)
%p: the precision of the output numbers
%n: the number of iteration
%ir: the irrational unit length, e.g. pi, exp(1), sqrt(2), etc.
r = 0; %Vector of advancing
c = 0; %Jumping number

for i = 1 : n
r = r + (c + 1) * ir; %Use c+1 to avoid stopping at c=0
x = r - floor(r); %Get decimal part
s = sprintf('%.*f',p,x) %Show decimal part and transfer to string
c = sscanf(s(p + 2),'%d'); %Get the last digit of decimal part
end

Here I simply test the \sqrt{5} seed at different iterations.

Well, my old computer starts to complain. Although double precision allows more than 10^{14} iterations, I would not push that hard.

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s