Lagged Fibonacci Generator


Reading time: about 4 minutes
In [1]:
get_ipython().ast_node_interactivity = 'all'
import os
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
import math
import collections
matplotlib.rcParams['figure.dpi'] = 150
In [2]:
class LFG:
    __slots__ = ('j', 'k', 'prev')
    def __init__(self, j, k):
        self.j = j
        self.k = k
        self.prev = collections.deque([i for i in range(k)], k)
        for _ in range(self.k * 1024):
            self()
    
    def __call__(self):
        val = self.prev[0] + self.prev[self.k - self.j]
        val &= 0xFFFFFFFFFFFFFFFF
        self.prev.append(val)
        return val >> 56
In [3]:
rng = LFG(24, 55)

print([rng() for _ in range(512)])
Out:
[187, 154, 20, 245, 18, 66, 96, 76, 243, 178, 21, 239, 38, 152, 103, 99, 57, 234, 145, 150, 211, 245, 15, 67, 238, 131, 203, 246, 86, 252, 43, 209, 187, 109, 134, 153, 121, 50, 170, 8, 129, 105, 22, 128, 48, 193, 38, 77, 70, 248, 111, 106, 217, 103, 212, 141, 85, 130, 124, 172, 187, 146, 246, 252, 52, 127, 6, 166, 201, 41, 137, 135, 48, 137, 5, 61, 207, 119, 23, 124, 217, 77, 115, 2, 184, 190, 200, 183, 161, 6, 160, 31, 251, 211, 146, 8, 154, 160, 133, 109, 145, 157, 101, 194, 209, 188, 221, 220, 31, 146, 85, 13, 35, 130, 76, 219, 141, 202, 142, 61, 26, 167, 44, 54, 187, 39, 236, 243, 91, 194, 26, 171, 151, 169, 209, 230, 113, 246, 79, 147, 75, 146, 70, 222, 32, 71, 75, 49, 142, 185, 245, 141, 252, 72, 136, 61, 52, 14, 147, 184, 45, 211, 43, 179, 222, 231, 83, 2, 163, 147, 39, 191, 89, 72, 50, 167, 163, 116, 191, 248, 92, 251, 134, 19, 239, 237, 215, 74, 136, 184, 58, 115, 153, 226, 186, 11, 235, 142, 17, 200, 234, 192, 240, 135, 21, 240, 20, 15, 56, 118, 20, 127, 151, 76, 242, 161, 108, 14, 109, 233, 211, 226, 19, 107, 126, 231, 176, 224, 94, 34, 188, 179, 172, 53, 12, 219, 146, 211, 5, 145, 90, 229, 184, 114, 140, 28, 135, 5, 97, 161, 187, 203, 236, 52, 132, 157, 108, 38, 148, 241, 131, 232, 21, 201, 209, 250, 55, 9, 216, 14, 40, 114, 111, 15, 165, 158, 206, 72, 240, 28, 84, 214, 116, 79, 166, 164, 200, 117, 6, 7, 18, 156, 172, 20, 185, 205, 85, 199, 23, 42, 235, 207, 245, 125, 246, 146, 64, 60, 218, 40, 102, 226, 44, 155, 3, 31, 148, 42, 131, 158, 79, 254, 33, 3, 249, 248, 103, 237, 5, 55, 223, 11, 34, 24, 130, 54, 2, 15, 83, 197, 56, 242, 249, 164, 86, 17, 189, 176, 14, 177, 52, 66, 205, 79, 10, 246, 241, 14, 0, 44, 148, 80, 143, 159, 97, 89, 219, 208, 241, 21, 221, 68, 56, 53, 210, 145, 203, 113, 13, 240, 233, 118, 237, 50, 204, 47, 154, 193, 122, 219, 18, 211, 1, 104, 162, 125, 43, 46, 119, 232, 221, 47, 189, 255, 155, 170, 47, 255, 28, 57, 144, 179, 136, 219, 63, 103, 81, 248, 66, 222, 132, 9, 71, 218, 242, 12, 2, 55, 208, 125, 193, 203, 141, 70, 129, 157, 254, 200, 113, 52, 128, 146, 4, 88, 96, 28, 26, 220, 91, 175, 127, 99, 254, 245, 169, 168, 188, 4, 128, 57, 169, 248, 113, 80, 186, 35, 184, 225, 60, 91, 130, 45, 83, 241, 94, 232, 8, 60, 132, 155, 201, 7, 184, 9, 39, 185, 60, 221, 1, 165, 85, 223, 5, 205, 183, 174, 229, 246, 182, 72, 36, 87, 96, 246, 120, 134, 27, 8, 28, 99, 229, 154]

The following pages link here

Citation

If you find this work useful, please cite it as:
@article{yaltirakliwikilaggedfibonaccigenerator,
  title   = "Lagged Fibonacci Generator",
  author  = "Yaltirakli, Gokberk",
  journal = "gkbrk.com",
  year    = "2024",
  url     = "https://www.gkbrk.com/wiki/lagged-fibonacci-generator/"
}
Not using BibTeX? Click here for more citation styles.
IEEE Citation
Gokberk Yaltirakli, "Lagged Fibonacci Generator", December, 2024. [Online]. Available: https://www.gkbrk.com/wiki/lagged-fibonacci-generator/. [Accessed Dec. 17, 2024].
APA Style
Yaltirakli, G. (2024, December 17). Lagged Fibonacci Generator. https://www.gkbrk.com/wiki/lagged-fibonacci-generator/
Bluebook Style
Gokberk Yaltirakli, Lagged Fibonacci Generator, GKBRK.COM (Dec. 17, 2024), https://www.gkbrk.com/wiki/lagged-fibonacci-generator/

Comments

© 2024 Gokberk Yaltirakli