Code: Select all
(define (random-sequence n m)
(letn ( s (sequence n m) j (length s) )
(dotimes (x (* j 10)) (swap (rand j) (rand j) s))
s ))
Code: Select all
(define (random-sequence n m)
(letn ( s (sequence n m) j (length s) )
(dotimes (x (* j 10)) (swap (rand j) (rand j) s))
s ))
Code: Select all
(define (random-sequence n m)
(letn (s (sequence n m) j (length s) result '())
(for (i j 1)
(push (pop s (rand i)) result))
result))
(random-sequence 1 10) => (1 6 8 9 10 3 5 7 2 4)
Code: Select all
(define (random-sequence n m)
(let (L (sequence 1 10))
(map (lambda (x) (pop L x)) (rand m m))))
Code: Select all
(define (random-sequence n m)
(let (L (sequence n m))
(map (lambda (x) (pop L x)) (rand m m))))
Code: Select all
(define (random-sequence n m)
(letn (L (sequence n m) N (length L))
(map (lambda (x) (pop L x)) (rand N N)) ))
Code: Select all
; chance that the sequence will be the same: (1 / N!)
(define (chance N)
(div 1 (exp (gammaln (+ N 1)))))
; N = 1..10
(for (x 1 10)
(println (format "N = %2d %9.5f" x (mul (chance x) 100)) " %"))
Code: Select all
N = 1 100.00000 %
N = 2 50.00000 %
N = 3 16.66667 %
N = 4 4.16667 %
N = 5 0.83333 %
N = 6 0.13889 %
N = 7 0.01984 %
N = 8 0.00248 %
N = 9 0.00028 %
N = 10 0.00003 %
Code: Select all
; n = 0..10
(define (factorial n)
('(1 1 2 6 24 120 720 5040 40320 362880 3628800) n))
Code: Select all
;
; nth possible combination of the list
;
(define (nth-combination lst i)
(let (len (length lst))
(if (or (< i 0) (>= i (factorial len)))
(throw-error "Bad index..."))
(if (<= len 1)
lst
(let (n 0 d 0 result '())
(dotimes (x len)
(setq n (factorial (- (- len x) 1)))
(setq d (/ i n))
(setq i (% i n))
(push (pop lst d) result -1))
result))))
Code: Select all
(define (new-randomize lst bool)
(let (len (length lst))
(if (<= len 1)
lst
(if bool
(nth-combination lst (rand (factorial len)))
(nth-combination lst (+ 1 (rand (- (factorial len) 1))))))))
Code: Select all
(setq L '(1 2 3))
(setq len (length L))
(println)
(dotimes (x (factorial len))
(println x " -> " (nth-combination L x)))
Code: Select all
0 -> (1 2 3)
1 -> (1 3 2)
2 -> (2 1 3)
3 -> (2 3 1)
4 -> (3 1 2)
5 -> (3 2 1)