Post Reply 
Shuffling arrays question
03-01-2015, 06:48 PM (This post was last modified: 03-01-2015 10:17 PM by Namir.)
Post: #5
RE: Shuffling arrays question
I am working on an array-shuffling algorithm that divides the array into ten "buckets". The method then merges two neighboring or distant buckets, shuffles them, and then copy the shuffled elements back to their original buckets. So far, I am able to get more randomness than the algorithm I posted earlier.

Here is the Matlab code. You can find this code integrated with a more comprehensive Matlab function in the part 3 article in my web site.

Code:
function x=unsort(x)
  N=length(x);
  delta=10;
  n=fix(N/delta);
  spacing=fix(delta)/2:-1:1;  
  for j=1:7
    for k=1:length(spacing)
      spc=spacing(k);
      for i=1:delta-1
        i1=1+(i-1)*n;
        i2=i1+n-1;
        i3=i1+spc*n;
        i4=i3+n-1;
        if i4>N, break; end
        if i1~=i3
          y=[x(i1:i2),x(i3:i4)];
          y=shuffle(y);
          x(i1:i2)=y(1:n);
          x(i3:i4)=y(n+1:2*n);
        end
      end
    end
  end
  %plot(x)
end

function x=shuffle(x)
  n=length(x);
  m=fix(n/2);
  primeArr=[1,primes(m)];
  for j=1:14
    for k=1:length(primeArr)
      aprime=primeArr(k);
      for i=1:aprime:n-aprime
        t=x(i);
        x(i)=x(i+aprime);
        x(i+aprime)=t;
      end
    end
  end
end
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Shuffling arrays question - Namir - 02-28-2015, 10:42 PM
RE: Shuffling arrays question - Paul Dale - 03-01-2015, 12:42 AM
RE: Shuffling arrays question - Namir - 03-01-2015, 01:10 PM
RE: Shuffling arrays question - Namir - 03-01-2015 06:48 PM
RE: Shuffling arrays question - BruceH - 03-07-2015, 05:56 AM



User(s) browsing this thread: 1 Guest(s)