Re: An Applied Algorithmic Activity:
- Posted by Matt Lewis <matthewwalkerlewis at yahoo.com> Jun 29, 2004
- 437 views
Patrick Barnes wrote: > > Lets say we want to step through the numbers 1 to N, in a particular > order. The order of the numbers can be arbitrary, but each number can > only be visited once, and the order of the visits should be seeded by > a number passed to it. > > For example: > 1923840657 > 0123456798 > 7648235910 > > It seems like it needs some sort of hash value, but I couldn't for the > life of me figure out how. > How would you suggest I do this? Each seed doesn't have to generate a > unique result, but it would be nice. One way to do this is to iterate using a number that is relatively prime to N. If two numbers are relatively prime, it means that their greatest common divisor is 1. So suppose N = 10. You could use 7, to pick a number at random, and you would get (modulo 10, of course): 7, 4, 1, 8, 5, 2, 9, 6, 3, 0 In euphoria:
integer N, n, p N = 10 p = 7 n = 0 for i = 1 to N do n = remainder( n + p, N ) ? n end for
Matt Lewis