Don't make weird id ints.

I thought I could generate names from these but it's just more cruft.
This commit is contained in:
Simon Forman 2024-04-14 14:36:23 -07:00
parent 157a0b8c60
commit 52fb54054e
2 changed files with 8 additions and 32 deletions

Binary file not shown.

View File

@ -35,7 +35,7 @@ Stars have what qualities?
''' '''
from random import randint, expovariate, seed, getrandbits, gauss from random import randint, expovariate, seed, gauss
from poisson import poisson from poisson import poisson
@ -50,28 +50,8 @@ WIDTH, HEIGHT = 10240, 7680
# Database stuff. SQL, etc. # Database stuff. SQL, etc.
def generate_list_of_u64_for_ids(n):
'''
Return a sorted list of n random 64-bit integers
(suitable for SQLite rowid columns.)
See https://www.sqlite.org/lang_createtable.html#rowid
'''
result = set()
safety = 10
while n > 0:
canidate = getrandbits(63)
while canidate in result:
if not safety:
raise RuntimeError('this should not happen')
safety -= 1
canidate = getrandbits(63)
result.add(canidate)
n -= 1
return sorted(result)
TABLES = [ TABLES = [
'create table stars (id INTEGER PRIMARY KEY, x INTEGER, y INTEGER, radius INTEGER)', 'create table stars (id INTEGER PRIMARY KEY AUTOINCREMENT, x INTEGER, y INTEGER, radius INTEGER)',
('''\ ('''\
create table planets ( create table planets (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
@ -88,9 +68,8 @@ def init_db(conn):
print('Generating star data.') print('Generating star data.')
c = conn.cursor() c = conn.cursor()
for values in generate_stars(WIDTH, HEIGHT, MINIMUM_DISTANCE_BETWEEN_STARS): for values in generate_stars(WIDTH, HEIGHT, MINIMUM_DISTANCE_BETWEEN_STARS):
c.execute('insert into stars values (?, ?, ?, ?)', values) c.execute('insert into stars(x, y, radius) values (?, ?, ?)', values)
star_id = values[0] generate_planets_for_star(c)
generate_planets_for_star(c, star_id)
c.close() c.close()
conn.commit() conn.commit()
@ -106,12 +85,8 @@ def iter_stars(conn):
def generate_stars(width, height, minimum_distance_between_stars): def generate_stars(width, height, minimum_distance_between_stars):
# we could just call getrandbits(64) here for ID and hope for the best. for (x, y) in poisson(width, height, minimum_distance_between_stars):
# it's a large domain (u64) so collisions are unlikely(!). yield x, y, round(1 + expovariate(1))
coords = list(poisson(width, height, minimum_distance_between_stars))
ids = generate_list_of_u64_for_ids(len(coords))
for star_id, (x, y) in zip(ids, coords):
yield star_id, x, y, round(1 + expovariate(1))
def how_many_planets(): def how_many_planets():
@ -120,7 +95,8 @@ def how_many_planets():
n = round(gauss(5, 5)) n = round(gauss(5, 5))
return n return n
def generate_planets_for_star(c, star_id): def generate_planets_for_star(c):
star_id = c.lastrowid
for ordo in range(1, how_many_planets() + 1): for ordo in range(1, how_many_planets() + 1):
c.execute('insert into planets(ordo, star) values (?, ?)', (ordo, star_id)) c.execute('insert into planets(ordo, star) values (?, ?)', (ordo, star_id))