Skip to content

Instantly share code, notes, and snippets.

@zane
Created July 17, 2010 02:57
Show Gist options
  • Save zane/479196 to your computer and use it in GitHub Desktop.
Save zane/479196 to your computer and use it in GitHub Desktop.
#lang racket
(define floors
'((1 . 10)
(2 . 3)
(3 . 25)
(4 . 100)))
(define (probability-list floors)
(cond [(empty? floors) empty]
[else (cons (cons (car (car floors))
(apply + (map cdr (cdr floors))))
(probability-list (cdr floors)))]))
(define (total floors)
(apply + (map cdr floors)))
(define (match-floor num floors)
(cond [(> num (cdr (car floors))) (car (car floors))]
[else (match-floor num (cdr floors))]))
(define (choose-floor floors)
(match-floor (random (total floors))
(probability-list floors)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment