Skip to content

Instantly share code, notes, and snippets.

@eslof
Created June 3, 2020 19:01
Show Gist options
  • Save eslof/88492e6a7c2eb90d61748227ab3b3fb1 to your computer and use it in GitHub Desktop.
Save eslof/88492e6a7c2eb90d61748227ab3b3fb1 to your computer and use it in GitHub Desktop.
Python ORDER BY Random with Seed in Sqlite3
import sqlite3
import random
def seeded_random_collation(string1, string2):
return random.randint(-1, 1)
try:
con = sqlite3.connect("db.sqlite")
except sqlite3.Error as e:
print(e)
else:
con.create_collation("seeded_random", seeded_random_collation)
cur = con.cursor()
random.seed("YOUR SEED HERE")
cur.execute(
"SELECT * FROM your_table ORDER BY CAST(id as TEXT) COLLATE seeded_random"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment