{"id":15156344,"url":"https://github.com/asg017/sqlite-fastrand","last_synced_at":"2025-10-24T13:30:40.051Z","repository":{"id":65520201,"uuid":"586060402","full_name":"asg017/sqlite-fastrand","owner":"asg017","description":"A SQLite extension for quickly generating random numbers, booleans, characters, and blobs","archived":false,"fork":false,"pushed_at":"2023-11-27T16:07:05.000Z","size":117,"stargazers_count":20,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-29T23:23:24.482Z","etag":null,"topics":["datasette-plugin","sqlite","sqlite-extension"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/asg017.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-01-06T20:46:06.000Z","updated_at":"2024-09-11T01:44:30.000Z","dependencies_parsed_at":"2023-11-27T17:28:40.329Z","dependency_job_id":"a508845e-c529-4263-a427-663aa318f9ca","html_url":"https://github.com/asg017/sqlite-fastrand","commit_stats":{"total_commits":25,"total_committers":1,"mean_commits":25.0,"dds":0.0,"last_synced_commit":"5238c990f7fb8f663337ef9cd6ece8dc2530e311"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asg017%2Fsqlite-fastrand","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asg017%2Fsqlite-fastrand/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asg017%2Fsqlite-fastrand/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asg017%2Fsqlite-fastrand/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/asg017","download_url":"https://codeload.github.com/asg017/sqlite-fastrand/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237973329,"owners_count":19395845,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["datasette-plugin","sqlite","sqlite-extension"],"created_at":"2024-09-26T19:04:08.550Z","updated_at":"2025-10-24T13:30:39.634Z","avatar_url":"https://github.com/asg017.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sqlite-fastrandom\n\nA SQLite extension for quickly generating random numbers, booleans, characters, and blobs. **Not cryptographically secure.** Based on [`sqlite-loadable-rs`](https://github.com/asg017/sqlite-loadable-rs) and the [fastrand crate](https://crates.io/crates/fastrand).`\n\nAccording to my local benchmarks, `fastrand_int64()` is about 2.6x faster than SQLite's `random()`, and `fastrand_blob()` is about 1.6x faster than `randomblob()`. `sqlite-fastrand` also offers a more ergonomic API with custom ranges, seeds, and boolean/character support. However, it yields psuedo-random results and isn't \"truly\" random.\n\nIf your company or organization finds this library useful, consider [supporting my work](#supporting)!\n\n## Usage\n\n```sql\n.load ./fastrand0\nselect fastrand_int(); -- 556823563\nselect fastrand_int(); -- 363294620\nselect fastrand_int(); -- -320463573\n```\n\nSet a seed for the underlying random number generator, for deterministic values.\n\n```sql\nselect fastrand_seed_set(1234);\nselect fastrand_int(); -- -2058591105\nselect fastrand_int(); -- -211244717\nselect fastrand_int(); -- -1772832958\n\nselect fastrand_seed_set(1234);\nselect fastrand_int(); -- -2058591105\nselect fastrand_int(); -- -211244717\nselect fastrand_int(); -- -1772832958\n```\n\nInclude `start` and `end` (exclusive) parameters to generate random numbers within a range.\n\n```sql\nselect fastrand_int(0, 10); -- 0\nselect fastrand_int(0, 10); -- 9\nselect fastrand_int(0, 10); -- 6\n```\n\nGenerate random digits, lowercase/uppercase/alphabetic/alphanumeric characters.\n\n```sql\nselect fastrand_alphabetic(); -- 's'\nselect fastrand_alphanumeric(); -- '2'\nselect fastrand_char(); -- '񠞼'\nselect fastrand_lowercase(); -- 'g'\nselect fastrand_uppercase();-- 'M'\n\nselect fastrand_digit(16); -- 'c'\n```\n\nGenerate a random float between 0 and 1.\n\n```sql\nselect fastrand_double(); -- 0.740834390248454\nselect fastrand_double(); -- 0.46936608707793\n```\n\n### Differences from `random()` and `randomblob()`\n\nThe builtin [`random()`](https://www.sqlite.org/lang_corefunc.html#random) and [`randomblob()`](https://www.sqlite.org/lang_corefunc.html#randomblob) are powerful tools that already exist in SQLite standard library, but they can be confusing at times.\n\nFor example, the `random()` function returns \"_... a pseudo-random integer between -9223372036854775808 and +9223372036854775807_\", which are the minimum and maximum values of a 64 bit signed integer.\n\n```sql\nselect random(); -- 8247412491507365610\nselect random(); -- 8124278049726255864\n```\n\nThis may work fine in your use-case, but typically I want a more constrained random number, like any number between `0-100`. This can technically be done with `random()` if you use `abs()` and the modulus `%` operator, but it gets awkward:\n\n```sql\nselect abs(random()) % 100; -- 96\nselect abs(random()) % 100; -- 41\n```\n\nThe `fastrand_int64()` function works the same as `random()` but offers an optional `start` and `end` parameters to specify a range in which the random number should be generated in.\n\n```sql\nselect fastrand_int64(); -- 5216671854996406003\nselect fastrand_int64(0, 100); -- 19\n```\n\n[`randomblob(N)`](https://www.sqlite.org/lang_corefunc.html#randomblob)\n\n\u003e _The randomblob(N) function return an N-byte blob containing pseudo-random bytes. If N is less than 1 then a 1-byte random blob is returned._\n\n```sql\nselect hex(randomblob(16)); -- '4E7EFDB9E687EED4F376359986CB695E'\nselect hex(randomblob(16)); -- 'F6CFF9249E3BD8755E10D6BB3CA81C66'\n```\n\nThe [`fastrand_blob(N)`](./docs.md#fastrand_blob) function acts in the same way.\n\n```sql\nselect hex(fastrand_blob(16)); -- 'D86FF5409D3FAD7DBE707580C7E7DE14'\nselect hex(fastrand_blob(16)); -- 'AB72BFE9480197F487933E8071072D4A'\n```\n\n## Installing\n\n| Language       | Install                                                              |                                                                                                                                                                                                     |\n| -------------- | -------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| Python         | `pip install sqlite-fastrand`                                        | [![PyPI](https://img.shields.io/pypi/v/sqlite-fastrand.svg?color=blue\u0026logo=python\u0026logoColor=white)](https://pypi.org/project/sqlite-fastrand/)                                                      |\n| Datasette      | `datasette install datasette-sqlite-fastrand`                        | [![Datasette](https://img.shields.io/pypi/v/datasette-sqlite-fastrand.svg?color=B6B6D9\u0026label=Datasette+plugin\u0026logoColor=white\u0026logo=python)](https://datasette.io/plugins/datasette-sqlite-fastrand) |\n| Node.js        | `npm install sqlite-fastrand`                                        | [![npm](https://img.shields.io/npm/v/sqlite-fastrand.svg?color=green\u0026logo=nodedotjs\u0026logoColor=white)](https://www.npmjs.com/package/sqlite-fastrand)                                                |\n| Deno           | [`deno.land/x/sqlite_fastrand`](https://deno.land/x/sqlite_fastrand) | [![deno.land/x release](https://img.shields.io/github/v/release/asg017/sqlite-fastrand?color=fef8d2\u0026include_prereleases\u0026label=deno.land%2Fx\u0026logo=deno)](https://deno.land/x/sqlite_fastrand)        |\n| Ruby           | `gem install sqlite-fastrand`                                        | ![Gem](https://img.shields.io/gem/v/sqlite-fastrand?color=red\u0026logo=rubygems\u0026logoColor=white)                                                                                                        |\n| Github Release |                                                                      | ![GitHub tag (latest SemVer pre-release)](https://img.shields.io/github/v/tag/asg017/sqlite-fastrand?color=lightgrey\u0026include_prereleases\u0026label=Github+release\u0026logo=github)                          |\n| Rust           | `cargo add sqlite-fastrand`                                          | [![Crates.io](https://img.shields.io/crates/v/sqlite-fastrand?logo=rust)](https://crates.io/crates/sqlite-fastrand)                                                                                 |\n\n\u003c!--\n| Elixir         | [`hex.pm/packages/sqlite_fastrand`](https://hex.pm/packages/sqlite_fastrand) | [![Hex.pm](https://img.shields.io/hexpm/v/sqlite_fastrand?color=purple\u0026logo=elixir)](https://hex.pm/packages/sqlite_fastrand)                                                                       |\n| Go             | `go get -u github.com/asg017/sqlite-fastrand/bindings/go`               | [![Go Reference](https://pkg.go.dev/badge/github.com/asg017/sqlite-fastrand/bindings/go.svg)](https://pkg.go.dev/github.com/asg017/sqlite-fastrand/bindings/go)                                     |\n--\u003e\n\nThe [Releases page](https://github.com/asg017/sqlite-fastrand/releases) contains pre-built binaries for Linux x86_64, MacOS, and Windows.\n\n### As a loadable extension\n\nIf you want to use `sqlite-fastrand` as a [Runtime-loadable extension](https://www.sqlite.org/loadext.html), Download the `fastrand0.dylib` (for MacOS), `fastrand0.so` (Linux), or `fastrand0.dll` (Windows) file from a release and load it into your SQLite environment.\n\n\u003e **Note:**\n\u003e The `0` in the filename (`fastrand0.dylib`/ `fastrand0.so`/`fastrand0.dll`) denotes the major version of `sqlite-fastrand`. Currently `sqlite-fastrand` is pre v1, so expect breaking changes in future versions.\n\nFor example, if you are using the [SQLite CLI](https://www.sqlite.org/cli.html), you can load the library like so:\n\n```sql\n.load ./fastrand0\nselect fastrand_version();\n-- v0.1.0\n```\n\nOr in Python, using the builtin [sqlite3 module](https://docs.python.org/3/library/sqlite3.html):\n\n```python\nimport sqlite3\ncon = sqlite3.connect(\":memory:\")\ncon.enable_load_extension(True)\ncon.load_extension(\"./fastrand0\")\nprint(con.execute(\"select fastrand_version()\").fetchone())\n# ('v0.1.0',)\n```\n\nOr in Node.js using [better-sqlite3](https://github.com/WiseLibs/better-sqlite3):\n\n```javascript\nconst Database = require(\"better-sqlite3\");\nconst db = new Database(\":memory:\");\ndb.loadExtension(\"./fastrand0\");\nconsole.log(db.prepare(\"select fastrand_version()\").get());\n// { 'fastrand_version()': 'v0.1.0' }\n```\n\nOr with [Datasette](https://datasette.io/):\n\n```\ndatasette data.db --load-extension ./fastrand0\n```\n\n## Supporting\n\nI (Alex 👋🏼) spent a lot of time and energy on this project and [many other open source projects](https://github.com/asg017?tab=repositories\u0026q=\u0026type=\u0026language=\u0026sort=stargazers). If your company or organization uses this library (or you're feeling generous), then please [consider supporting my work](https://alexgarcia.xyz/work.html), or share this project with a friend!\n\n## See also\n\n- [sqlite-xsv](https://github.com/asg017/sqlite-xsv), A SQLite extension for working with CSVs\n- [sqlite-loadable](https://github.com/asg017/sqlite-loadable-rs), A framework for writing SQLite extensions in Rust\n- [sqlite-http](https://github.com/asg017/sqlite-http), A SQLite extension for making HTTP requests\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasg017%2Fsqlite-fastrand","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasg017%2Fsqlite-fastrand","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasg017%2Fsqlite-fastrand/lists"}