This is a bit of an odd question, but I wondered if someone had come across something like it before. I have a list of lists of a string and a weight value i.e. '(("a" 1) ("b" 4))
and I need to randomly select one of the strings based on the weight. So in the above example I should get "a"
20% of the time and "b"
80% of the time. I’m not asking for code to do this, just some hints as to an approach, as all my ideas so far are far from nice.
I’d just convert it into a list without weights and randomly select
like, make duplicates according to weights, basically
That could work, could get quite large if there are a lot of choices but I guess it might not be too bad.
Alternatively, use the weights to make a map of ranges to strings, where no range overlaps and the size of the range is proportional to the weight, and then randomly select between the overall range and look up the map
That would keep the potential size down, I quite like that.
@hoshom I like your second idea, I’ll give that a go, Thanks.
Is there any way for DrRacket to show test coverage info for normal #lang racket or typed racket programs? It does show coverage for student languages
I found a package for coverage, I wonder if it integrates with DrRacket
@mark.warren another idea would be to convert the list to a cumulative-weight list, choose a random value within the range of the maximum cumulative weight, and do a search for the corresponding element. In your example, the cumulative list would be (("a" 1) ("b" 5))
@diego Thanks for that idea, I have implemented the second idea from @hoshom and it’s working nicely. I might implement your idea as well and see which works best though.
#lang racket/base
(define (silly-random-by-weight vals+weights)
(apply sync
(for*/list ([v+w (in-list vals+weights)]
[v (in-value (car v+w))]
[w (in-value (cadr v+w))]
[n (in-range w)])
(define ch (make-channel))
(thread (λ ()
(sleep (random))
(channel-put ch v)))
ch)))
:smile:
is in a Friday mood
ahahah
What? It’s kind of like “sleep sort”. You know, from The Little Book of Abusing Threads and Sleep.
I guess I’ll gain enlightment by.. sleeping on it.
@greg Wow, thanks
@greg Next up. A silly-sort.
@mark.warren what you’re doing is the same as sampling from a discrete (not necessarily uniform) probability distribution. the suggestion by @diego is how this is usually done
I think the math libraries include a probability distribution module, maybe using that would be easy?
@javicasma has joined the channel