shuffle.ss
#lang scheme/base

(require (only-in (planet vyzo/crypto:2) random-bytes))

(define (shuffle l)  
  (define num-bytes (ceiling (/ (log (length l)) (log 2) 8)))
  ;(define num-bytes 1)
  
  (define (random-number . blah)
    (let loop ((left num-bytes) (amount 8) (result 0))
      (if (or (< amount 1) (< left amount))
          (if (< amount 1)
              result
              (loop left (/ amount 2) result))
          (if (= amount 1)
              (loop (- left 1) 1 (+ result (bytes-ref (random-bytes 1) 0)))
              (loop (- left amount) amount (+ result (integer-bytes->integer (random-bytes amount) #f)))))))
  
  (sort l < #:key random-number))

(provide shuffle)