Created
June 3, 2020 19:01
-
-
Save eslof/88492e6a7c2eb90d61748227ab3b3fb1 to your computer and use it in GitHub Desktop.
Python ORDER BY Random with Seed in Sqlite3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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