https://github.com/bwesterb/powercycle
Generates a not-so-pseudorandom cyclic permutation using modular exponentiation
https://github.com/bwesterb/powercycle
cycle golang modular-exponentiation permutation pseudocode
Last synced: 7 months ago
JSON representation
Generates a not-so-pseudorandom cyclic permutation using modular exponentiation
- Host: GitHub
- URL: https://github.com/bwesterb/powercycle
- Owner: bwesterb
- Created: 2017-11-22T13:49:32.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-29T09:16:34.000Z (about 3 years ago)
- Last Synced: 2024-06-19T10:05:46.491Z (over 1 year ago)
- Topics: cycle, golang, modular-exponentiation, permutation, pseudocode
- Language: Go
- Size: 6.84 KB
- Stars: 8
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
powercycle
==========
Go package to generate a (not-so\*) pseudo-random cycle.
Example
-------
```go
package main
import (
"fmt"
"github.com/bwesterb/powercycle"
)
func main() {
var x uint64
cycle := powercycle.New(10)
for i := 0; i < 10; i++ {
fmt.Println(x)
x = cycle.Apply(x)
}
}
```
might output
```
0
6
4
1
2
9
3
5
8
7
```
Not-so pseudorandom
-------------------
For efficiency, the cycles generated are of a very particular form. So do
not use this package if you want to have a real pseudo-random cycle.
How it works
------------
To generate a cycle of size n, we find a prime p > n + 1 such that
furthermore (p - 1)/2 is also a prime. This makes it easy to find a
generator g modulo p. The action of the cycle is usually given by
x ---> (((x + 1) * g) % p) - 1
As often p > n + 1, this might yield a number bigger than n.
In that case the action is repeated.