全然関係ないけど。「リストをランダムに並べ変える」。
(use srfi-1) (use math.mt-random) (use gauche.sequence) (define (list-randomize ls) (define (remove-with-index n ls) (reverse (fold-with-index (^ (i e acc) (if (= i n) acc (cons e acc))) '() ls))) (define rand (let1 m (make <mersenne-twister> :seed (sys-time)) (^ (size) (mt-random-integer m size)))) (define (randomize ls) (let rec ((ls ls)(acc '())) (if (null? ls) acc (let1 idx (rand (length ls)) (rec (remove-with-index idx ls) (cons (list-ref ls idx) acc)))))) (randomize ls)) (list-randomize (iota 10)) ;; -> (0 1 2 5 8 9 7 6 3 4) (list-randomize (iota 10)) ;; -> (4 3 0 8 7 6 5 9 2 1)
0 件のコメント:
コメントを投稿