#!/bin/env tclsh package require sqlite3 package require tcl::chan::random package require tcl::randomseed proc fatal {reason {code -1}} { puts stderr "$argv0: fatal: $reason" exit $code } proc usage {} { global argv0 puts stderr "usage: $argv0 DB-PATH" exit -1 } if {[llength $argv] != 1} usage set dbpath [lindex $argv 0] if {[catch {sqlite3 db $dbpath -create false}]} { fatal "can't open \"$dbpath\"" } set unplayed [db eval {SELECT name,href FROM titles WHERE played ISNULL AND ignore = 0}] set chan [::tcl::chan::random [::tcl::randomseed]] binary scan [read $chan 4] i1 n set idx [expr { (abs($n) % ([llength $unplayed] / 2)) * 2 }] set name [lindex $unplayed $idx] set href [lindex $unplayed $idx+1] puts "Random pick: $name" puts "- $href"