Created
July 17, 2010 02:57
-
-
Save zane/479196 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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