It does. Here’s ten runs. fact-example-syntax.rkt> (random-sample (list 1 2 3) 3 #:replacement? #f)
'(1 2 3)
fact-example-syntax.rkt> (random-sample (list 1 2 3) 3 #:replacement? #f)
'(1 2 3)
fact-example-syntax.rkt> (random-sample (list 1 2 3) 3 #:replacement? #f)
'(1 2 3)
fact-example-syntax.rkt> (random-sample (list 1 2 3) 3 #:replacement? #f)
'(1 2 3)
fact-example-syntax.rkt> (random-sample (list 1 2 3) 3 #:replacement? #f)
'(1 2 3)
fact-example-syntax.rkt> (random-sample (list 1 2 3) 3 #:replacement? #f)
'(1 2 3)
fact-example-syntax.rkt> (random-sample (list 1 2 3) 3 #:replacement? #f)
'(1 2 3)
fact-example-syntax.rkt> (random-sample (list 1 2 3) 3 #:replacement? #f)
'(1 2 3)
fact-example-syntax.rkt> (random-sample (list 1 2 3) 3 #:replacement? #f)
'(1 2 3)
fact-example-syntax.rkt> (random-sample (list 1 2 3) 3 #:replacement? #f)
'(1 2 3)
There does seem to be a bug in random-sample
. Running the following repeatedly: (random-sample '(1 2 3 4) 3 #:replacement? #f)
gives results where the first element is 1
or 4
, never 2
or 3
I think this is the intended behavior of random-sample
: “Returns a list of n elements of seq, picked at random, listed in any order.” So, if you pick 3 elements of a sequence that has only 3 elements, the only choice is to get all of them. And since they can be returned in any order, the original order is valid.
Ah. Yes, I had a preconceived notion of what to expect.
And the “in any order” part means that if the first element of a 4-element sequence is picked, it’s ok to always report it as the first element of the result.
I’ve never looked at this function before, and I’m still unclear on what #:replacement?
is supposed to do. The documentation probably needs to be improved and have examples.
Maybe bold the listed in any order part :)
With replacement would allow picking the same element more than once.
Upon re-reading the doc after your comment, it’s technically accurate, but it wouldn’t hurt to emphasize the fact that the sample is not shuffled.
Yes, I think I see now.Probably the docs could be better than “with replacement means with replacement”, just in case someone is unclear on what “with replacement” means in the first place.
There was a long discussion of this recently, either on the mailing list or github
Oh yeah, now I remember people talking about how if you want them in a random order, you can shuffle them afterward. Forgot about that.
How can I match-define ’diamonds and the 7 in '(A (diamonds . 7))
. If I change the pair for a list, I can do it with (match-define (list _ (list suit val)) '(A (diamonds 7)))
Oops! Just figured it out. I need a cons: (match-define (list _ (cons suit val)) '(A (diamonds . 7)))
(First time using match. Thanks!)
Is there a base library procedure for applying a procedure various times as in (f (f (f x)))?
I can’t seem to find exactly what you’re looking for, but I think it should be doable with a fold. Here is an example if I am understanding what you’re asking about correctly:
#lang racket
(define (apply-times seed proc times)
(for/fold ([result seed])
([i (range times)])
(proc result)))
(define (double number)
(* 2 number))
(apply-times 3 double 5)
> 96
Alternatively, you can frame the issue as calculating a fixpoint if you expect it to eventually converge. I recently had to do this and used:
(define (converge proc x)
(define step (proc x))
(cond [(<= step 0) 0]
[else (+ step (converge proc step))]))
Do either of those examples make sense or help?
Your first approach seems to be what I’m doing. But instead of for/fold I’m writing the recursion explicitly. Have a look. (Thanks for looking into it. I should probably use for/fold.) (define (n-rounds n a-game)
(cond [(= n 0) a-game]
[else
(n-rounds (sub1 n) (table-one-round a-game))]))
I should abstract table-one-round as a procedure argument.