[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference rusure::math

Title:Mathematics at DEC
Moderator:RUSURE::EDP
Created:Mon Feb 03 1986
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2083
Total number of notes:14613

366.0. "Wanted: permutation of 1..N" by AURORA::HALLYB () Tue Oct 22 1985 00:32

I have an array A[I], I = 1..N that I would like to initialize to a permutation 
of the integers 1..N in what would pass for random order.  Nothing rigorous,
just an apparent random order.

Yes, it is possible to initialize the array with A[I] = I and then shuffle the
elements using standard techniques.  I was wondering if anyone knew of a way
of doing this without generating random numbers, and without multiprecision.

N is not necessarily prime.

  John
T.RTitleUserPersonal
Name
DateLines
366.1METOO::YARBROUGHTue Oct 22 1985 11:466
You might try using something like the Shell Sort permutation, i.e. for the
largest power of 2, K, <= N, interchange all the elements that are K apart,
then all those K/2 apart, then K/3,... It's probably not necessary to 
interchange adjacent elements to get a reasonable degree of randomness.

Lynn 
366.2BEING::POSTPISCHILTue Oct 22 1985 12:424
Out of curiosity, what is wrong with random numbers?


				-- edp
366.3AURORA::HALLYBTue Oct 22 1985 15:1112
There's no "problem" with random numbers or Lynn's algorithm in .1, except
they make an implicit assumption that all A[I] fits in memory or is fairly
quickly randomly accessed.

I would prefer a one-pass algorithm, so that the A[I] could be generated and
written out to magnetic tape in (say) 4K chunks.  So actual "shuffling" of
data would be quite difficult.

I keep thinking there's some approach, such as setting A[I] = f(i) mod N,
that does what I want.  I just don't know what the "f()" to do.  :-)

  John
366.4R2ME2::GILBERTTue Oct 22 1985 16:4117
You probably (no pun intended) want something like a linear congruential
random-number generator that cycles every n numbers.  See Knuth, volume 2:

Theorem A.  The linear congruential sequence:
		X(n+1) = (aX(n)+c) mod m
	has a period of length m if and only if:

    i) c is relatively prime to m;
   ii) b = a-1 is a multiple of p, for every p dividing m;
  iii) b is a multiple of 4, if m is a multiple of 4.

Of course, if you have a random number generator that generates the numbers
0..m-1 before cycling, and you just want the numbers 0..n-1 (where n < m),
you can use it, and just 'skip over' any numbers >= n.

It sounds like you have a 'toy' use for this.  So, just grab some numbers
that fit the above conditions, and let it fly.
366.5TOOLS::STANTue Oct 22 1985 16:544
Nijenhuis and Wilf (Combinatorial Algorithms), gives an algorithm
called RANPER, random permutation of n letters, on page 62.
However, I've looked at the algorithm and it does random interchanges,
which is not what you are looking for.
366.6METOO::YARBROUGHWed Oct 23 1985 12:1718
Oh, you want to do this for LARGE N. If you know in advance what N is, you
could try this: Find the largest p<N that is prime, and output
	  2
	(i + c) mod p	for i<p where c is some arbitrary constant
	i		for i>p

The numbers from p to N will appear in order, but all the others will be 
scrambled. You can, of course, rearrange p-N.

If you don't know in advance what N will be, but have an upper limit n for
N, then pick p>=n and output
	  2
	(i + c) mod p	if it's <N
	nothing		if it's >=N
for i = 1..p or until you have filled the array. (This may take a large
amount of time, but it's predictable.)

Lynn Yarbrough
366.7AURORA::HALLYBWed Oct 23 1985 13:213
Thanks, everybody.  I'm going to try Gilbert's .4 and see how well that works.

  John
366.8R2ME2::GILBERTWed Oct 23 1985 17:095
Re 366.6:
          2
The set {i  mod p}, for prime p, doesn't contain p elements (except for p=2).
                                                      2        2
In fact, it contains at most (p+1)/2 elements, since x  = (p-x) , modulo p.
366.9METOO::YARBROUGHThu Oct 24 1985 11:312
You're right, I should have said i*c1+c2 mod p. Of course, there are a lot
of other mappings that will work. - Lynn
366.10ADVAX::J_ROTHThu Oct 24 1985 11:459
			  i
If you generate the set {g  mod p} where g is a primitive root of a prime
p, it runs thru the integers 1 to (p-1), in some cases this could be a 
possbility.  The quadratic residue method only generates (p-1)/2
nonzero integers, though the sequence does have some nice properties
for digital signal processing (as does the primitive root sequence
here).

- Jim
366.11AURORA::HALLYBThu Oct 24 1985 21:3612
OK, the linear congruential method is satisfactory for my purposes.  I should
note, however, that when 100 | N the rightmost digit forms a periodic sequence.
That is, they loop in a pattern 7,2,5,8,3,0,1,4,9,6 (for example).  This is
in fact a known property of linear-congruential random nuber generators -- the
rightmost digits (bits) aren't always very random.  I also tried the quadratic
congruential generator, where X(n+1) = d * X(n) ^ 2 + a * X(n) + c (mod N) which
I got from the same source.  It has the same problem with the right-hand digits.
This makes me wonder that for i | N, the digits (mod i) of the random sequence
aren't random if the random sequence is generated by any polynomial-version
congruential generator.  I suspect Knuth has that written down somewhere...

  John