{"id":13507278,"url":"https://github.com/pma/lfsr","last_synced_at":"2026-03-11T06:31:33.620Z","repository":{"id":20465604,"uuid":"23743002","full_name":"pma/lfsr","owner":"pma","description":"Elixir implementation of a binary Galois LFSR","archived":false,"fork":false,"pushed_at":"2016-11-28T13:21:30.000Z","size":15,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-21T17:50:52.777Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pma.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-09-06T19:17:37.000Z","updated_at":"2021-10-20T18:36:41.000Z","dependencies_parsed_at":"2022-07-31T20:48:05.469Z","dependency_job_id":null,"html_url":"https://github.com/pma/lfsr","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pma/lfsr","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pma%2Flfsr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pma%2Flfsr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pma%2Flfsr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pma%2Flfsr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pma","download_url":"https://codeload.github.com/pma/lfsr/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pma%2Flfsr/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30373452,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-11T06:09:32.197Z","status":"ssl_error","status_checked_at":"2026-03-11T06:09:17.086Z","response_time":84,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T02:00:29.826Z","updated_at":"2026-03-11T06:31:33.289Z","avatar_url":"https://github.com/pma.png","language":"Elixir","funding_links":[],"categories":["Algorithms and Data structures","Algorithms and Datastructures"],"sub_categories":[],"readme":"# LFSR\n\nElixir implementation of a binary Galois Linear Feedback Shift Register.\n\nIt can be used to generate out-of-order counters, since a LFSR of size n can\ngenerate a sequence of length 2\u003csup\u003en\u003c/sup\u003e-1 (without repetitions).\n\n## Usage\n\nAdd LFSR as a dependency in your `mix.exs` file.\n\n```elixir\ndef deps do\n  [{:lfsr, \"~\u003e 0.0.2\"}]\nend\n```\n\nAfter you are done, run `mix deps.get` in your shell to fetch and compile LFSR. Start an interactive Elixir shell with `iex -S mix`.\n\n```iex\niex\u003e lfsr = LFSR.new(1, 16)\n%LFSR{mask: 46080, state: 1}\niex\u003e lfsr = lfsr |\u003e LFSR.next |\u003e LFSR.next\n%LFSR{mask: 46080, state: 23040}\niex\u003e LFSR.state(lfsr)\n23040\n```\n\n### Usage with Streams\n\nLet's generate a pseudo-random sequence of numbers between 1_000_000 and 9_999_999.\n\nWe need a 24-bit LFSR, since with a 23-bit we can only produce a sequence with length 8_388_607.\n\n```iex\niex\u003e LFSR.new(5_345_234, 24) \\\n...\u003e |\u003e Stream.iterate(\u0026LFSR.next/1) \\\n...\u003e |\u003e Stream.map(\u0026LFSR.state/1) \\\n...\u003e |\u003e Stream.filter(\u0026(\u00261 in 1_000_000..9_999_999)) \\\n...\u003e |\u003e Enum.take(100)\n[5345234, 2672617, 6697466, 3348733, 6342207, 9312455, 9930289, 9683736,\n 4841868, 2420934, 1210467, 5787404, 2893702, 1446851, 5816952, 2908476,\n 1454238, 8610569, 5036226, 2518113, 6658840, 3329420, 1664710, 6002284,\n 3001142, 1500571, 5823667, 5315478, 2657739, 6230457, 8111214, 4055607,\n 8764486, 4382243, 5397444, 2698722, 1349361, 6890940, 3445470, 1722735,\n 8472877, 5001803, 5474889, 7922322, 3961161, 6495314, 3247657, 6316938,\n 3158469, 6294641, ...]\n```\n\n### Usage with Agents\n\n```elixir\ndefmodule PRNG do\n  def start_link do\n    Agent.start_link(fn -\u003e LFSR.new(5_345_234, 24) end, name: __MODULE__)\n  end\n\n  def next do\n    Agent.get_and_update(__MODULE__, fn lfsr -\u003e\n      lfsr = next_valid(lfsr)\n      {lfsr.state, lfsr}\n    end)\n  end\n\n  defp next_valid(%LFSR{} = lfsr) do\n    lfsr = LFSR.next(lfsr)\n    if lfsr.state in 1_000_000..9_999_999, do: lfsr, else: next_valid(lfsr)\n  end\nend\n```\n\n```iex\niex\u003e PRNG.start_link\n{:ok, #PID\u003c0.101.0\u003e}\niex\u003e PRNG.next\n2672617\niex\u003e PRNG.next\n6697466\n```\n## References\n\n* http://en.wikipedia.org/wiki/Maximum_length_sequence\n* http://en.wikipedia.org/wiki/Linear_feedback_shift_register#Galois_LFSRs\n* http://www.eej.ulst.ac.uk/~ian/modules/EEE515/files/old_files/lfsr/lfsr_table.pdf","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpma%2Flfsr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpma%2Flfsr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpma%2Flfsr/lists"}